![Android开发:从0到1 (清华开发者书库)](https://wfqqreader-1252317822.image.myqcloud.com/cover/572/26943572/b_26943572.jpg)
7.5 复选框
“复选框”有两个用途:一是多个“选项”,可以为用户提供多个选择项;二是单个“选项”,可以为用户提供两种状态切换的控件,类似于ToggleButton和Switch。
7.5.1 CheckBox
在Android中,“复选框”是CheckBox, CheckBox默认样式如图7-20所示。
![](https://epubservercos.yuewen.com/73B1DF/15367246905322506/epubprivate/OEBPS/Images/Figure-0121-0126.jpg?sign=1738867051-zJlg85rgYZlJbsMq1JjKAR4fsnd56jH1-0-33fcffd0b79e1cc1786f52b9791e9f42)
图7-20 CheckBox默认样式
CheckBox对应类是android.widget.CheckBox,类图如图7-21所示,从图中可见android.widget.CheckBox继承了android.widget.Button,这说明CheckBox是一种按钮。CheckBox有一个特有属性android:checked,该属性设置CheckBox的选中状态。它是一个布尔值,true表示选中,false表示未选中。
![](https://epubservercos.yuewen.com/73B1DF/15367246905322506/epubprivate/OEBPS/Images/Figure-0121-0127.jpg?sign=1738867051-N2Lu9jGGam5Hgqn4L7cRA7B4KWyx4L3j-0-1eeb89865f9b13279924de0632a30e0f)
图7-21 CheckBox类图
7.5.2 实例:使用复选框
图7-22是使用复选框实例,在屏幕上出现了4个CheckBox选项,可以单击进行选择。
![](https://epubservercos.yuewen.com/73B1DF/15367246905322506/epubprivate/OEBPS/Images/Figure-0121-0128.jpg?sign=1738867051-MycjJztMSaN9TXBwOKh9k5ehv3toP7jP-0-f3e75bedf00ce1b92419a094ff8da56b)
图7-22 使用复选框实例
布局文件activity_main.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/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" ① android:text="@string/checkBox01" android:textSize="@dimen/size"/> <CheckBox android:id="@+id/CheckBox02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/checkBox02" android:textSize="@dimen/size"/> <CheckBox android:id="@+id/CheckBox03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/checkBox03" android:textSize="@dimen/size"/> <CheckBox android:id="@+id/CheckBox04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/checkBox04" android:textSize="@dimen/size"/> </LinearLayout>
在布局文件中声明了4个CheckBox,代码第①行android:checked="true"是设置CheckBox01复选框被选中。
MainActivity.java代码如下:
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{ ① CheckBox mCheckbox1, mCheckbox2, mCheckbox3, mCheckbox4; TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView=(TextView) findViewById(R.id.TextView01); //注册CheckBox01 监听器 mCheckbox1 =(CheckBox) findViewById(R.id.CheckBox01); mCheckbox1.setOnCheckedChangeListener(this); ② //注册CheckBox2 监听器 mCheckbox2 =(CheckBox) findViewById(R.id.CheckBox02); mCheckbox2.setOnCheckedChangeListener(this); //注册CheckBox03 监听器 mCheckbox3 =(CheckBox) findViewById(R.id.CheckBox03); mCheckbox3.setOnCheckedChangeListener(this); //注册CheckBox04监听器 mCheckbox4 =(CheckBox) findViewById(R.id.CheckBox04); mCheckbox4.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ ③ CheckBox ckb =(CheckBox)buttonView; mTextView.setText(ckb.getText()); } }
上述代码第①行是声明MainActivity实现CompoundButton.OnCheckedChangeListener接口,这样MainActivity就成为了CheckBox事件监听器,该接口要求实现代码第③行的onCheckedChanged方法。代码第②行注册当前MainActivity作为CheckBox01监听器,类似其他三个CheckBox都需要注册。
这4个CheckBox事件处理都是在代码第③行实现的onCheckedChanged方法中完成,如果每一个CheckBox处理不同,这需要判断是哪一个CheckBox,类似代码参考7.4.3节的RadioButton实例。
注意 CheckBox的事件处理要实现CompoundButton.OnCheckedChangeListener接口,这与RadioButton不同,RadioButton的事件处理者要实现的接口是RadioGroup. OnCheckedChangeListener。他们的事件源差别很大,CheckBox事件源是android. widget.CheckBox,而RadioButton的事件源是android.widget.RadioGroup,不是RadioButton。