Android系统控件CheckBox是一个同时可以选择多个选项的控件;而RadioButton则是仅可以选择一个选项的控件;RadioGroup是RadioButton的承载体,程序运行时不可见,应用程序中可能包含一个或多个RadioGroup,一个RadioGroup包含多个RadioButton,在每个RadioGroup中,用户仅能够选择其中一个RadioButton。
        下面就通过一个例子来加深对这两个控件的理解,其效果如图-1所示。
        
图-1  CheckBox与RadioButton效果图
        1.建立一个“CheckboxRadiobuttonDemo”程序
        程序包含5个控件,从上至下分别是TextView01、CheckBox01、 CheckBox02、RadioButton01、RadioButton02,当选择RadioButton01时,RadioButton02则无法选择。
        CheckboxRadiobuttonDemo在XML文件中的代码如代码清单1所示。
        代码清单1  main.xml
        <TextView android:id="@+id/TextView01“
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:text="@string/hello"/>
            <CheckBox android:id="@+id/CheckBox01"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="CheckBox01" >
            </CheckBox>
            <CheckBox android:id="@+id/CheckBox02" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:text="CheckBox02" >
            </CheckBox>
            <RadioGroup android:id="@+id/RadioGroup01"
 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
                <RadioButton android:id="@+id/RadioButton01" 
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:text="RadioButton01" >
                </RadioButton>
                <RadioButton android:id="@+id/RadioButton02" 
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:text="RadioButton02" >
                </RadioButton>
            </RadioGroup>
        
        上述代码中,第15行标签声明了一个RadioGroup;在第18行和第23行分别声明了两个RadioButton,这两个RadioButton是RadioGroup的子元素。
        2.引用CheckBox和RadioButton
        引用CheckBox和RadioButton的方法参考代码清单2所示的代码。
        代码清单2  引用CheckBox和RadioButton
        CheckBox checkBox1= (CheckBox)findViewById(R.id.CheckBox01);
            RadioButton radioButton1 =(RadioButton)findViewById(R.id.RadioButton01);
        3.响应点击事件:添加点击事件的监听器
        CheckBox设置点击事件监听器的方法与Button设置点击事件监听器中介绍的方法相似,唯一不同在于将Button.OnClickListener换成了CheckBox.OnClickListener。
        代码清单3  设置CheckBox点击事件监听器
        CheckBox.OnClickListener checkboxListener = new CheckBox.OnClickListener(){
                @Override
                public void onClick(View v) {
                //过程代码
            }};
            checkBox1.setOnClickListener(checkboxListener);
            checkBox2.setOnClickListener(checkboxListener);
        
        RadioButton设置点击事件监听器的方法如代码清单4所示。
        代码清单4  设置RadioButton点击事件监听器
        RadioButton.OnClickListener radioButtonListener = new RadioButton.OnClickListener(){
                @Override
                public void onClick(View v) {
                //过程代码
            }};
            radioButton1.setOnClickListener(radioButtonListener);
            radioButton2.setOnClickListener(radioButtonListener);
        
        通过上述的讲解,可以得出这样的结论:CheckBox是可以选择多个选项的复选框控件,当其中选项被选中时,显示相应的checkmark。这时,需要创建一个“OnClickListener”捕获点击事件,并可以添加所需的功能代码。
        RadioGroup是一个包含一些RadioButton的ViewGroup。用户可选择一个按钮,通过对每一个RadioButton设置监听OnClickListeners来获取其选择。这里需注意,点击RadioButton并不触发RadioGroup的Click事件。