Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 入門小案例:單選框 RadioGroup 與多選框 CheckBox 全解

編輯:關於 Android 程式設計

Android 裡的單選框,效果其實和 HTML 中的一模一樣。要實現它,我們需要用到兩個控件:RadioGroup 與 CheckBox。廢話不多說,直接上程式碼。

首先是 radio.xml 布局檔案:

           

布局效果如下:

接著是 string.xml 檔案,把所有字串資源集中管理:

  Hello World, Activity07! activity07 男 女 swim run read  

核心的邏輯都在 RadioTest 這個 Activity 裡。它先聲明各控件物件,透過 findViewById 綁定,再為 RadioGroup 和每個 CheckBox 分別掛上監聽器:

 package mars.activity07; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class RadioTest extends Activity { private RadioGroup genderGroup = null; private RadioButton femaleButton = null; private RadioButton maleButton = null; private CheckBox swimBox = null; private CheckBox runBox = null; private CheckBox readBox = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio); genderGroup = (RadioGroup)findViewById(R.id.genderGroup); femaleButton = (RadioButton)findViewById(R.id.femaleButton); maleButton = (RadioButton)findViewById(R.id.maleButton); swimBox = (CheckBox)findViewById(R.id.swim); runBox = (CheckBox)findViewById(R.id.run); readBox = (CheckBox)findViewById(R.id.read); genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if(femaleButton.getId() == checkedId){ Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show(); } else if(maleButton.getId() == checkedId) { Toast.makeText(RadioTest.this, "male", Toast.LENGTH_SHORT).show(); } } }); swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { System.out.println("swim is checked"); } else { System.out.println("swim is unchecked"); } } }); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { System.out.println("run is checked"); } else { System.out.println("run is unchecked"); } } }); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked) { System.out.println("read is checked"); } else { System.out.println("read is unchecked"); } } }); } }

這裡有兩個關鍵點值得留心:RadioGroup 用的是 RadioGroup.OnCheckedChangeListener,它與 Button 的監聽器不同,回調裡能拿到當前選中的 checkedId,藉此判斷用戶選了哪個單選項;而每個 CheckBox 用的是 CompoundButton.OnCheckedChangeListener,回調中的 isChecked 參數能告訴你它是「從未選中變為選中」還是「從選中變為未選中」。

別忘了在清單檔案中註冊這個 Activity:

    

最終運行效果如下:

是不是一目了然?單選框的互斥選擇與多選框的自由勾選,就這樣輕鬆搞定了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved