編輯:關於 Android 程式設計

RadioButton(單選按鈕)與 CheckBox(復選按鈕)是表單類界面的核心元件:前者用於「多選一」的情境(例如性別),後者用於「可多選」的情境(例如興趣標籤)。兩者通常搭配容器與事件監聽器使用。

效果圖:

RadioButton 必須放進 RadioGroup 中,才能發揮「同組互斥」的特性。實作程式碼如下:
public class RadioButtonTest extends Activity {
private RadioGroup Rgroup;
private Button Rbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_button_test);
Rgroup = (RadioGroup) findViewById(R.id.sex);
Rbutton = (Button) findViewById(R.id.SexId);
Rbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int len = Rgroup.getChildCount(); // 取得單選按鈕組的選項個數
String msgString = "";
for (int i = 0; i < len; i++) {
RadioButton radioButton = (RadioButton) Rgroup.getChildAt(i);
if (radioButton.isChecked()) {
msgString = radioButton.getText().toString();
break;
}
}
Toast.makeText(RadioButtonTest.this, msgString, Toast.LENGTH_LONG).show();
}
});
}
}
程式碼解析
這裡替「選擇性別」按鈕綁定了 setOnClickListener,每次點擊就遍歷一次 RadioGroup,判斷哪個按鈕被選中。取得 RadioButton 資訊的常用方法有:
getChildCount():取得按鈕組中單選按鈕的數目getChildAt(i):依索引取得單選按鈕isChecked():判斷按鈕是否被選中上述寫法是把取值動作綁在其他 Button 上。其實也可以直接綁在 RadioButton 自身的監聽器上:



RadioButton radbtn = (RadioButton) findViewById(R.id.rad_male);
radbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(RadioButtonTest.this,
"你選了" + radbtn.getText(), Toast.LENGTH_LONG).show();
}
});

兩種做法的差別在於觸發時機:綁在 Button 上是「按下確認才讀取結果」,綁在 RadioButton 上則是「選中的瞬間即回應」。可依互動設計需求挑選。

運行結果:

CheckBox 預設是未勾選狀態,若要改預設值,可在 XML 中把 android:checked 設為 true,也可以在程式碼中呼叫 setChecked()。
/**
* CheckBox 預設為未選中狀態;若要改預設值,
* 可將 XML 中的 android:checked 設為 true,
* 或使用 CheckBox.setChecked() 設定,兩者都能達到復選效果。
*/
public class CheckBoxTest extends Activity implements OnClickListener {
private List<CheckBox> checkBoxs = new ArrayList<CheckBox>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String cString[] = new String[]{
"你是誰", "我是要成為架構師的男人", "你什麼時候才可以", "我馬上就會成為"
};
// 動態載入版面
LinearLayout linearLayout = (LinearLayout) getLayoutInflater()
.inflate(R.layout.check_box_test, null);
// 為每個 checkbox 賦值
for (int i = 0; i < cString.length; i++) {
CheckBox checkBox = (CheckBox) getLayoutInflater().inflate(R.layout.checkbox, null);
checkBoxs.add(checkBox);
checkBoxs.get(i).setText(cString[i]);
linearLayout.addView(checkBox, i);
}
setContentView(linearLayout);
Button button = (Button) findViewById(R.id.ensurenbutton);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String s = "";
for (CheckBox checkBox : checkBoxs) {
if (checkBox.isChecked()) {
s += checkBox.getText() + "\n";
}
}
if (" ".equals(s)) {
s = "你還沒有選中項!!";
}
// 用提示框把結果顯示給使用者
new AlertDialog.Builder(this)
.setMessage(s)
.setPositiveButton("關閉", null)
.show();
}
}
這個寫法的優點是:把 CheckBox 集合起來一次遍歷,適合選項數量不固定、需要動態生成的情況。
若希望勾選的瞬間就有回饋,可以改用 setOnCheckedChangedListener(XML 部分太簡單,此處省略):





運行圖:

實作步驟是準備兩張狀態圖(選中/未選中),再用 selector 描述文件把它们綁到 android:button 或 android:drawableLeft 上:



預設的勾選框在文字左側,若設計稿要求改成其他方向,可用 android:button 搭配 drawable 屬性調整:

兩者距離可透過 android:paddingLeft/drawablePadding 等屬性微調:

效果如下:

掌握以上幾點,RadioButton 與 CheckBox 的常見需求基本都能應付;更複雜的多選樣式則可考慮改用 RecyclerView 搭配自訂 Item 版面。
Android AsyncTask 異步任務加載圖片實戰與源碼解析
一、為什麼需要異步任務 Android 的 UI 線程如果被阻塞超過 5 秒,就會引發 ANR(Application Not Responding)異常;而如果用戶等待
Android 自定義軟鍵盤:打造屬於你的安全輸入面板
前不久項目需要一個自定義軟鍵盤,我上網翻了翻,覺得大多數方案都偏繁瑣,於是決定自己動手實現一個以備不時之需。這裡參考的是百度錢包的軟鍵盤,看著還不錯: 一、SoftIn
Android 免 Root 實作 APK 靜默安裝的原理與做法
靜默安裝聽起來很誘人,但一般做法很難實現:Android 並沒有開放對外的 Intent 直接完成安裝,最常見的寫法其實會跳出安裝介面。 Intent intent =
Android RecyclerView 使用全解:從原理到實戰的完整梳理
自 Android 5.0 之後,Google 推出了 RecyclerView 控制項。看到這個新名詞,多數人會先冒出三個問題:它是什麼?相較 ListView 有什麼