- Android開發(fā):從0到1 (清華開發(fā)者書庫)
- 趙志榮
- 576字
- 2020-11-28 16:13:40
7.5 復(fù)選框
“復(fù)選框”有兩個用途:一是多個“選項”,可以為用戶提供多個選擇項;二是單個“選項”,可以為用戶提供兩種狀態(tài)切換的控件,類似于ToggleButton和Switch。
7.5.1 CheckBox
在Android中,“復(fù)選框”是CheckBox, CheckBox默認(rèn)樣式如圖7-20所示。

圖7-20 CheckBox默認(rèn)樣式
CheckBox對應(yīng)類是android.widget.CheckBox,類圖如圖7-21所示,從圖中可見android.widget.CheckBox繼承了android.widget.Button,這說明CheckBox是一種按鈕。CheckBox有一個特有屬性android:checked,該屬性設(shè)置CheckBox的選中狀態(tài)。它是一個布爾值,true表示選中,false表示未選中。

圖7-21 CheckBox類圖
7.5.2 實例:使用復(fù)選框
圖7-22是使用復(fù)選框?qū)嵗谄聊簧铣霈F(xiàn)了4個CheckBox選項,可以單擊進行選擇。

圖7-22 使用復(fù)選框?qū)嵗?/p>
布局文件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"是設(shè)置CheckBox01復(fù)選框被選中。
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 監(jiān)聽器 mCheckbox1 =(CheckBox) findViewById(R.id.CheckBox01); mCheckbox1.setOnCheckedChangeListener(this); ② //注冊CheckBox2 監(jiān)聽器 mCheckbox2 =(CheckBox) findViewById(R.id.CheckBox02); mCheckbox2.setOnCheckedChangeListener(this); //注冊CheckBox03 監(jiān)聽器 mCheckbox3 =(CheckBox) findViewById(R.id.CheckBox03); mCheckbox3.setOnCheckedChangeListener(this); //注冊CheckBox04監(jiān)聽器 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實現(xiàn)CompoundButton.OnCheckedChangeListener接口,這樣MainActivity就成為了CheckBox事件監(jiān)聽器,該接口要求實現(xiàn)代碼第③行的onCheckedChanged方法。代碼第②行注冊當(dāng)前MainActivity作為CheckBox01監(jiān)聽器,類似其他三個CheckBox都需要注冊。
這4個CheckBox事件處理都是在代碼第③行實現(xiàn)的onCheckedChanged方法中完成,如果每一個CheckBox處理不同,這需要判斷是哪一個CheckBox,類似代碼參考7.4.3節(jié)的RadioButton實例。
注意 CheckBox的事件處理要實現(xiàn)CompoundButton.OnCheckedChangeListener接口,這與RadioButton不同,RadioButton的事件處理者要實現(xiàn)的接口是RadioGroup. OnCheckedChangeListener。他們的事件源差別很大,CheckBox事件源是android. widget.CheckBox,而RadioButton的事件源是android.widget.RadioGroup,不是RadioButton。
- Developing Middleware in Java EE 8
- 云原生Spring實戰(zhàn)
- 軟件架構(gòu):Python語言實現(xiàn)
- 零基礎(chǔ)學(xué)單片機C語言程序設(shè)計
- Unity&VR游戲美術(shù)設(shè)計實戰(zhàn)
- C語言程序設(shè)計與應(yīng)用(第2版)
- MATLAB GUI純代碼編寫從入門到實戰(zhàn)
- uni-app跨平臺開發(fā)與應(yīng)用從入門到實踐
- Building Dynamics CRM 2015 Dashboards with Power BI
- Software-Defined Networking with OpenFlow(Second Edition)
- 深入理解Java虛擬機:JVM高級特性與最佳實踐
- C語言程序設(shè)計
- 計算機常用算法與程序設(shè)計教程(第2版)
- Python程序設(shè)計:基礎(chǔ)與實踐
- Learning Node.js for Mobile Application Development