第五章 用戶界面基礎(chǔ)(CheckBox)

參考資料:
《Android應(yīng)用程序開發(fā)》ISBN 9787302283164
參考軟件:
Android Studio、Eclipse+ADT、Android SDK、JDK
用戶界面基礎(chǔ)(接上節(jié)內(nèi)容)
六、CheckBox
CheckBox復(fù)選按鈕是一種有雙狀態(tài)按鈕的特殊類型,可以選中或者不選中??梢袁F(xiàn)在布局文件中定義多選按鈕,然后對(duì)每一個(gè)多選按鈕進(jìn)行事件監(jiān)setOnCheckedChangeListener,通過isChecked來判斷選項(xiàng)是否被選中
?下面是具體的例子:
?MainActivity.java
2.
3.??import?android.app.Activity;
4.??import?android.os.Bundle;
5.??import?android.widget.CheckBox;
6.??import?android.widget.CompoundButton;
7.??import?android.widget.Toast;
8.??import?android.widget.CompoundButton.OnCheckedChangeListener;
9.
10.public?class?MainActivity?extends?Activity{
11.????//聲明復(fù)選按鈕
12.????private?CheckBox?cBox1;
13.????private?CheckBox?cBox2;
14.????private?CheckBox?cBox3;
15.
16.????@Override
17.????public?void?onCreate(Bundle?savedInstanceState){
18.????????super.onCreate(savedInstanceState);
19.????????setContentView(R.layout.main);
20.????????//通過findViewById獲得CheckBox對(duì)象
21.????????cBox1=(CheckBox)findViewById(R.id.checkbox1);
22.????????cBox2=(CheckBox)findViewById(R.id.checkbox2);
23.????????cBox3=(CheckBox)findViewById(R.id.checkbox3);
24.
25.????????//注冊(cè)事件監(jiān)聽器
26.????????cBox1.setOnCheckedChangeListener(listener);
27.????????cBox2.setOnCheckedChangeListener(listener);
28.????????cBox3.setOnCheckedChangeListener(listener);
29.
30.????}
31.????//響應(yīng)事件
32.????private?OnCheckedChangeListener?listener?=?new?OnCheckedChangeListener(){
33.????????@Override
34.????????public?void?onCheckedChanged(CompoundButton?buttonView,?boolean?isChecked)
35.????????{
36.????????????//cBox1被選中
37.????????????if?(buttonView.getId()==R.id.checkbox1){
38.????????????????if?(isChecked){
39.????????????????????Toast.makeText(MainActivity.this,?"你喜歡足球",?Toast.LENGTH_LONG).show();
40.????????????????}
41.????????????}
42.????????????//cBox2被選中
43.????????????else?if?(buttonView.getId()==R.id.checkbox2){
44.????????????????if?(isChecked){
45.????????????????????Toast.makeText(MainActivity.this,?"你喜歡籃球",?Toast.LENGTH_LONG).show();
46.????????????????}
47.????????????}
48.????????????//cBox3被選中
49.????????????else?if?(buttonView.getId()==R.id.checkbox3){
50.????????????????if?(isChecked){
51.????????????????????Toast.makeText(MainActivity.this,?"你喜歡排球",?Toast.LENGTH_LONG).show();
52.????????????????}
53.????????????}
54.????????}
55.????};
56.}
main.xml
2.??<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
3.??????android:orientation="vertical"
4.??????android:layout_width="fill_parent"
5.??????android:layout_height="fill_parent"
6.??????>
7.??????<TextView
8.??????????android:layout_width="fill_parent"
9.??????????android:layout_height="wrap_content"
10.????????android:text="@string/hello"
11.????????android:textSize="20sp"
12.????????android:textStyle="bold"
13.????????android:textColor="#FFFFFF"
14.????????/>
15.????<CheckBox
16.????????android:id="@+id/checkbox1"
17.????????android:layout_width="wrap_content"
18.????????android:layout_height="wrap_content"
19.????????android:text="@string/football"
20.????????android:textSize="16sp"
21.????????/>
22.????<CheckBox
23.????????android:id="@+id/checkbox2"
24.????????android:layout_width="wrap_content"
25.????????android:layout_height="wrap_content"
26.????????android:text="@string/basketball"
27.????????android:textSize="16sp"
28.????????/>
29.????<CheckBox
30.????????android:id="@+id/checkbox3"
31.????????android:layout_width="wrap_content"
32.????????android:layout_height="wrap_content"
33.????????android:text="@string/volleyball"
34.????????android:textSize="16sp"
35.????????/>
36.</LinearLayout>
strings.xml
2.??<resources>
3.??????<stringname="hello">你喜歡的運(yùn)動(dòng)是</string>
4.??????<stringname="app_name">復(fù)選按鈕測試</string>
5.??????<stringname="football">足球</string>
6.??????<stringname="basketball">籃球</string>
7.??????<stringname="volleyball">排球</string>
8.??</resources>
效果圖:
?
