因为需要,需要自己定义个CheckBox。
1.定义checkboxtestxml.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <CheckBox
- android:id="@+id/checkboxxml_CB1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/mcheckbox"
- android:button="@null"
- />
- </LinearLayout>
2.定义里面的mcheckbox.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:state_checked="true" android:drawable="@drawable/checkbox_select"></item>
- <item android:state_selected="true" android:drawable="@drawable/checkbox_select"></item>
- <item android:state_checked="false" android:drawable="@drawable/checkbox"></item>
- </selector>
3.java代码:
- import com.login.login.R;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.Toast;
- public class CheckboxTestActivity extends Activity {
- private CheckBox checkboxxml_CB1;
- private Context context = CheckboxTestActivity.this;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.checkboxtestxml);
- init();
- }
- private void init() {
- checkboxxml_CB1 = (CheckBox) findViewById(R.id.checkboxxml_CB1);
- checkboxxml_CB1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if(isChecked){
- Toast.makeText(context, "选择了", 2).show();
- }else{
- Toast.makeText(context, "没选择", 2).show();
- }
- }
- });
- }
- }
图片资源直接在百度上面找吧,就2个,一个是选中状态的,一个是没选中的。
注意点:
1.另外必须将android:button设置为@null