CheckBox继承自CompoundButton,是有双状态按钮的复选框,可以选中也可 以不选中。CheckBox没有组的概念,可以独立使用。可以为CheckBox添加监听器 ,在监听器的代码中需要复写public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)方法,方法的两个参数分别为CompoundButton 和选中状态,因为CheckBox继承自CompoundButton,是CompoundButton的子类, 所以可以转化为CompoundButton。下面我们同样通过一个例子来介绍CheckBox的使用。

main.xml

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
 <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;vertical&quot; >

     <TextView
        android:layout_width=&quot;fill_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;请选择你的爱好&quot; />
     <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;fill_parent&quot;
    android:orientation=&quot;horizontal&quot; >
     <CheckBox
         android:id=&quot;@+id/ck1&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:text=&quot;编程&quot;/>
      <CheckBox
         android:id=&quot;@+id/ck2&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:text=&quot;睡觉&quot;/>
       <CheckBox
         android:id=&quot;@+id/ck3&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:text=&quot;游戏&quot;/>
        <CheckBox
         android:id=&quot;@+id/ck4&quot;
         android:layout_width=&quot;wrap_content&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:text=&quot;泡妞&quot;/>
      </LinearLayout>
      <Button
         android:id=&quot;@+id/btn&quot;
         android:layout_width=&quot;fill_parent&quot;
         android:layout_height=&quot;wrap_content&quot;
         android:layout_marginTop=&quot;10dp&quot;
         android:text=&quot;提交&quot;/>
 </LinearLayout>

CheckBoxDemoActivity.java

package com.liuzhichao.CheckBox;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CheckBoxDemoActivity extends Activity {

	private CheckBox ck1;
	private CheckBox ck2;
	private CheckBox ck3;
	private CheckBox ck4;
	private Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findView();
        setListeners();
    }

    //通过控件的ID来得到代表控件的对象
    private void findView(){
    	 ck1 = (CheckBox)findViewById(R.id.ck1);
         ck2 = (CheckBox)findViewById(R.id.ck2);
         ck3 = (CheckBox)findViewById(R.id.ck3);
         ck4 = (CheckBox)findViewById(R.id.ck4);
         btn = (Button)findViewById(R.id.btn);
    }

    private void setListeners(){

    	 //给ck1设置事件监听
    	ck1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					Toast.makeText(getApplicationContext(), &quot;你选中了&quot;+buttonView.getText(), 0).show();
				}
			}
		});
    	//给ck2设置事件监听
    	ck2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					Toast.makeText(getApplicationContext(), &quot;你选中了&quot;+buttonView.getText(), 0).show();
				}
			}
		});
    	//给ck3设置事件监听
    	ck3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					Toast.makeText(getApplicationContext(), &quot;你选中了&quot;+buttonView.getText(), 0).show();
				}
			}
		});
    	//给ck4设置事件监听
    	ck4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					Toast.makeText(getApplicationContext(), &quot;你选中了&quot;+buttonView.getText(), 0).show();
				}
			}
		});
    	//给btn设置事件监听
    	btn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String str = &quot;你的爱好是:&quot;;
				if (ck1.isChecked()) {
					str+=&quot; &quot;+ck1.getText();
				}
				if (ck2.isChecked()) {
					str+=&quot; &quot;+ck2.getText();
				}
				if (ck3.isChecked()) {
					str+=&quot; &quot;+ck3.getText();
				}
				if (ck4.isChecked()) {
					str+=&quot; &quot;+ck4.getText();
				}
				Toast.makeText(getApplicationContext(), str, 0).show();
			}
		});
    }
}

运行效果如下:

CheckBox
CheckBox