public abstract class
CompoundButton
extends Button
implements Checkable
java.lang.Object
? android.view.View
? android.widget.TextView
? android.widget.Button
? android.widget.CompoundButton
Known Direct Subclasses
CheckBox, RadioButton, Switch, ToggleButton
可以看出CheckBox和RadioButton同樣都是CompoundButton抽象類(lèi)的子類(lèi)。其中CheckBox一般用于多選的情形,RadioButton一般用于單選的情形。注意,若想要RadioButton實(shí)現(xiàn)單選功能,必須用一個(gè)RadionGroup標(biāo)簽包裹所有的RadioButton。 這兩個(gè)控件的使用都是比較簡(jiǎn)單的,下面通過(guò)一個(gè)實(shí)例來(lái)對(duì)這兩個(gè)控件的方法和屬性進(jìn)行全面的學(xué)習(xí)。 主布局文件(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">
<TextView
android:layout_margin="10dp"
android:layout_width="match_parent"
android:text="請(qǐng)選擇您的性別:"
android:textSize="18sp"
android:layout_height="wrap_content" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/boy"
android:text="男"
android:checked="true"
android:layout_margin="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/girl"
android:text="女"
android:layout_margin="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<TextView
android:layout_margin="10dp"
android:layout_width="match_parent"
android:text="請(qǐng)選擇您的愛(ài)好:"
android:textSize="18sp"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/cb_climb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="19sp"
android:text="爬山"
android:button="@drawable/checkbox_bg"
android:checked="true" />
<CheckBox
android:id="@+id/cb_football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="19sp"
android:text="足球"
android:button="@drawable/checkbox_bg"/>
<CheckBox
android:id="@+id/cb_swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="19sp"
android:text="游泳"
android:button="@drawable/checkbox_bg"
android:checked="true" />
<Button
android:id="@+id/btn_ok"
android:onClick="commit"
android:text="確定"
android:gravity="center"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:padding="10dp"
android:text="選擇結(jié)果"
android:textSize="18sp"
android:layout_height="wrap_content" />
</LinearLayout>
這里對(duì)CheckBox的外觀(guān)進(jìn)行了自定義,設(shè)置了其button屬性為:"@drawable/checkbox_bg"。下面看一下checkbox_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked" android:state_checked="true" />
<item android:drawable="@drawable/unchecked" android:state_checked="false" />
</selector>
Selector標(biāo)簽進(jìn)行包裹,主要用來(lái)設(shè)置控件背景,其中每一個(gè)item代表一個(gè)狀態(tài)。設(shè)置了屬性state_checked為true或false時(shí)分別對(duì)應(yīng)不同的圖片。 除了state_checked屬性還有如下常用屬性: ? android:state_selected控件是否被選中 ? android:state_focused控件是否獲得焦點(diǎn) ? android:state_pressed控件是否被點(diǎn)擊 ? android:state_enabled控件是否可用
MainActivity.java代碼(MainActivity.java)
public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
private CheckBox checkBoxClimb, checkBoxFootBall, checkBoxSwim;
private RadioButton radioButtonBoy, radioButtonGirl;
private String string = new String();
private Set<String> set = new HashSet<String>();
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initText();
}
//不做選擇直接按確定按鈕時(shí)
private void initText() {
if (radioButtonGirl.isChecked()) {
string ="性別:女、";
} else {
string="性別:男、";
}
if (checkBoxClimb.isChecked()) {
set.add("爬山");
if (checkBoxSwim.isChecked()) {
set.add("游泳");
if (checkBoxFootBall.isChecked()) {
set.add("足球");
}
}
}
}
//初始化控件,設(shè)置事件監(jiān)聽(tīng)
private void initViews() {
textView = (TextView) findViewById(R.id.tv_result);
checkBoxClimb = (CheckBox) findViewById(R.id.cb_climb);
checkBoxFootBall = (CheckBox) findViewById(R.id.cb_football);
checkBoxSwim = (CheckBox) findViewById(R.id.cb_swim);
radioButtonBoy = (RadioButton) findViewById(R.id.boy);
radioButtonGirl = (RadioButton) findViewById(R.id.girl);
checkBoxClimb.setOnCheckedChangeListener(this);
checkBoxFootBall.setOnCheckedChangeListener(this);
checkBoxSwim.setOnCheckedChangeListener(this);
radioButtonBoy.setOnCheckedChangeListener(this);
radioButtonGirl.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.boy:
if (isChecked) {
string="性別:男、";
}
break;
case R.id.girl:
if (isChecked) {
string="性別:女、";
}
break;
case R.id.cb_climb:
if (isChecked) {
set.add("爬山");
}else{
set.remove("爬山");
}
break;
case R.id.cb_football:
if (isChecked) {
set.add("足球");
}else{
set.remove("足球");
}
break;
case R.id.cb_swim:
if (isChecked) {
set.add("游泳");
}else {
set.remove("游泳");
}
break;
}
}
public void commit(View view) {
textView.setText(string.toString() + "愛(ài)好:" + set.toString());
}
}
Activity實(shí)現(xiàn)了CompoundButton.OnCheckedChangeListener接口,覆寫(xiě)了onCheckedChanged方法,有參數(shù)CompoundButton的對(duì)象buttonView結(jié)合其getId方法獲取所有選擇控件的ID,根據(jù)ID的不同區(qū)分不同的選項(xiàng)按鈕。 在邏輯處理方面要注意,這里采用了Set集合介紹愛(ài)好中的字符串,Set集合不接收重復(fù)的元素,可以滿(mǎn)足本實(shí)例的需求。其中initText方法用于初始化選擇內(nèi)容。 運(yùn)行實(shí)例如下:
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/4-1.png" alt="這里寫(xiě)圖片描述" />
http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/images/4-2.png" alt="這里寫(xiě)圖片描述" />
可以看出CheckBox的外觀(guān)是我們添加的圖片,選擇完成后點(diǎn)擊確定按鈕即可輸出選擇的性別和愛(ài)好。