編輯:Android 技術基礎
這一節帶來 Android 基本 UI 控件中的 RadioButton 和 Checkbox。要講解的內容包括:1. 基本用法;2. 事件處理;3. 自定義點擊效果;4. 改變文字與選擇框的相對位置;5. 修改文字與選擇框的距離。
這兩個控件有很多相似之處,除了單選與多選的區別,其他幾乎一致。另外關於 ListView 上 Checkbox 錯位的問題,我們會在 ListView 那一章單獨解決。開始本節內容。官方文檔:RadioButton;CheckBox。
顧名思義,單選按鈕只能選中一個,所以我們要把 RadioButton 放進 RadioGroup 按鈕組裡,才能實現單選。先看一個簡單的性別選擇例子。另外可以給外層 RadioGroup 設置 orientation 屬性,來控制排列方向是垂直還是水平。
效果圖:

PS:筆者手機是 Android 5.0.1,這裡的 RadioButton 比舊版本好看一些。
布局代碼:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="請選擇性別" android:textSize="23dp" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/btnMan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:checked="true"/> <RadioButton android:id="@+id/btnWoman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女"/> </RadioGroup> <Button android:id="@+id/btnpost" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交"/> </LinearLayout>
獲得選中的值有兩種方法。
第一種是為 RadioGroup 設置監聽器 setOnCheckedChangeListener:
RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup); //第一種獲得單選按鈕值的方法 //為radioGroup設置一個監聽器:setOnCheckedChanged() radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton radbtn = (RadioButton) findViewById(checkedId); Toast.makeText(getApplicationContext(), "按鈕組值發生改變,你選了" + radbtn.getText(), Toast.LENGTH_LONG).show(); } });
運行效果圖: 

PS:切記一定要給每個 RadioButton 都加上 id,否則單選功能會失效!!!
第二種方法是點擊其他按鈕時再獲取值,當然也可以隨時直接獲取,看需求而定:
Button btnchange = (Button) findViewById(R.id.btnpost); RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup); //為radioGroup設置一個監聽器:setOnCheckedChanged() btnchange.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { for (int i = 0; i < radgroup.getChildCount(); i++) { RadioButton rd = (RadioButton) radgroup.getChildAt(i); if (rd.isChecked()) { Toast.makeText(getApplicationContext(), "點擊提交按鈕,獲取你選擇的是:" + rd.getText(), Toast.LENGTH_LONG).show(); break; } } } });
運行效果圖:

代碼解析: 這裡給提交按鈕設了 setOnClickListener,每次點擊就遍歷一次 RadioGroup 判斷哪個被選中。可以用下面這些方法拿到 RadioButton 的相關信息:
復選框可以同時選中多個選項。獲得選中值同樣有兩種方式:1. 為每個 CheckBox 添加 setOnCheckedChangeListener;2. 弄個按鈕,點擊後遍歷判斷每個 checkbox 的 isChecked()。
運行效果圖:

實現代碼:
public class MainActivity extends AppCompatActivity implements View.OnClickListener,CompoundButton.OnCheckedChangeListener{ private CheckBox cb_one; private CheckBox cb_two; private CheckBox cb_three; private Button btn_send; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb_one = (CheckBox) findViewById(R.id.cb_one); cb_two = (CheckBox) findViewById(R.id.cb_two); cb_three = (CheckBox) findViewById(R.id.cb_three); btn_send = (Button) findViewById(R.id.btn_send); cb_one.setOnCheckedChangeListener(this); cb_two.setOnCheckedChangeListener(this); cb_three.setOnCheckedChangeListener(this); btn_send.setOnClickListener(this); } @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if(compoundButton.isChecked()) Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show(); } @Override public void onClick(View view) { String choose = ""; if(cb_one.isChecked())choose += cb_one.getText().toString() + ""; if(cb_two.isChecked())choose += cb_two.getText().toString() + ""; if(cb_three.isChecked())choose += cb_three.getText().toString() + ""; Toast.makeText(this,choose,Toast.LENGTH_SHORT).show(); } }
5.0 之後的 RadioButton 和 Checkbox 雖然比舊版好看,但未必合每個人的口味或需求,這時可以自己定製點擊效果。實現很簡單:先寫一個 selector 資源,設置選中與未選中時切換的圖片。
實現效果圖:

PS:受限於素材,圖看著有點小。
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="https://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_checked="true" android:drawable="@mipmap/ic_checkbox_checked"/> <item android:state_enabled="true" android:state_checked="false" android:drawable="@mipmap/ic_checkbox_normal" /> </selector>
寫好後有兩種設置方式:
① 用 android:button 屬性指向這個 selector:
android:button="@drawable/rad_btn_selctor"
② 在 style 中定義一個屬性,再用 android style 引用。先往 style 添加:
<style name="MyCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox"> <item name="android:button">@drawable/rad_btn_selctor</item> </style>
然後在布局中通過 style 屬性引用即可。
這個也很好實現。還記得之前學 TextView 時用到的 drawableXxx 嗎?控制選擇框位置兩步搞定:
Step 1. android:button="@null"
Step 2. android:drawableTop="@android:drawable/btn_radio"
當然,drawableXxx 也可以換成你喜歡的圖案。
有時我們想讓文字和選擇框別挨太緊,可以:
1. 在 XML 裡用 android:paddingXxx = "xxx" 控制距離;
2. 在 Java 代碼裡動態計算 paddingLeft,更靈活一些。
示例代碼:
rb.setButtonDrawable(R.drawable.rad_btn_selctor); int rb_paddingLeft = getResources().getDrawable(R.mipmap.ic_checkbox_checked).getIntrinsicWidth()+5; rb.setPadding(rb_paddingLeft, 0, 0, 0);
關於 RadioButton 和 Checkbox 就講到這裡。如果有寫得不到位的地方,或者有更好的建議,歡迎指出,不勝感激~謝謝。
Fragment 實作:新聞與購物 App 的列表詳情切換教學
為什麼列表頁常使用 Fragment?在許多新聞、購物與內容型 App 中,使用者點擊列表項目後,右側或下方會切換成詳情內容。這類介面若只用單一 Activity 切換,
8.4.3 屬性動畫入門:ValueAnimator 與 ObjectAnimator
本節導讀 本節談 Android 的第三種動畫——屬性動畫(Property Animation)。 先釐清一個常見混淆點。上一節為 Fragment 設定過場動畫時提過
第74章、再識 Intent — 呼叫系統寄送彩信(從零開始學 Android)
本章示範如何透過 Intent 完成彩信(MMS)的發送。做法同樣是呼叫系統內建的訊息程式,並在 Intent 中帶入收件號碼、內文與要附加的圖片資源。 一、設計介面
Android Activity 深入:Task、返回堆疊與啟動模式
Activity、Window 與 View 的關係 Activity 負責提供使用者介面,Window 則是視窗的抽象概念,而 View 是實際繪製畫面的元件。Acti