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

Android RadioGroup 與 RadioButton 的基本用法

編輯:關於 Android 程式設計

RadioGroup 與 RadioButton 是 Android 中實作單選控制的標準組合。它的運作模式與多數 UI 框架類似:把幾個 RadioButton 放進同一個 RadioGroup,群組會自動保證「最多只有一個被選中」,並透過 OnCheckedChangeListener 通知你哪一個被選了。

程式碼

 import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Radio extends Activity {

  private TextView myTextView;
  private RadioButton chinaBtn;
  private RadioButton ukBtn;
  private RadioButton usaBtn;
  private RadioGroup rg;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // 以 ID 找出各元件
    myTextView = (TextView) findViewById(R.id.myTextView);

    chinaBtn = (RadioButton) findViewById(R.id.china_Button);
    ukBtn    = (RadioButton) findViewById(R.id.uk_Button);
    usaBtn   = (RadioButton) findViewById(R.id.usa_Button);

    rg = (RadioGroup) findViewById(R.id.rBtnGroup);

    // 監聽要設在 RadioGroup 上,而不是個別的 RadioButton
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (R.id.china_Button == checkedId) {
          myTextView.setText("您選擇的國家是:" + chinaBtn.getText().toString());
        } else if (R.id.uk_Button == checkedId) {
          myTextView.setText("您選擇的國家是:" + ukBtn.getText().toString());
        } else if (R.id.usa_Button == checkedId) {
          myTextView.setText("您選擇的國家是:" + usaBtn.getText().toString());
        }
      }
    });
  }
} 

版面配置

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

  <RadioGroup
    android:id="@+id/rBtnGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
      android:id="@+id/china_Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="中國" />

    <RadioButton
      android:id="@+id/uk_Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="英國" />

    <RadioButton
      android:id="@+id/usa_Button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="美國" />
  </RadioGroup>
</LinearLayout> 

幾個重點與實務建議

監聽掛在 RadioGroup 上

這是最容易搞錯的地方。RadioButton 本身繼承自 CompoundButton,也有 setOnCheckedChangeListener,但在 RadioGroup 裡應該監聽群組。原因是:當使用者從 A 切到 B 時,A 會先觸發「取消勾選」,若你監聽的是個別按鈕,就會收到兩次回呼,狀態判斷變得很麻煩。監聽群組則只會給你「最終被選中的那一個」。

用 checkedId 判斷,不要用文字比對

上例用 if-else 逐一比對 ID,這是教學寫法。實務上更乾淨的方式是先把選項與資料綁在一起:

 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
    switch (checkedId) {
      case R.id.china_Button: selectedCountry = "CN"; break;
      case R.id.uk_Button:    selectedCountry = "UK"; break;
      case R.id.usa_Button:   selectedCountry = "US"; break;
    }
    myTextView.setText("您選擇的國家是:" + group.findViewById(checkedId).getTag());
  }
}); 

預設選項與動態建立

要預設勾選某一項,可以在 XML 中對該 RadioButton 加上 android:checked="true",或在程式碼中呼叫 rg.check(R.id.uk_Button)。

若要動態產生選項,直接 new 出 RadioButton 並 rg.addView() 即可,群組的互斥邏輯仍然有效:

 RadioButton rb = new RadioButton(this);
rb.setId(View.generateViewId());
rb.setText("新選項");
rg.addView(rb); 

單選與多選不要混用

「只能選一個」用 RadioGroup;「可以多選」應該用 CheckBox 或 CheckedTextView。RadioGroup 只能容納 RadioButton 作為直接子元件,若你塞進其他 View,群組的互斥行為就不保證了。

另外提醒,RadioButton 的 buttonTint(API 21 以上可用 android:buttonTint)能改勾選圓點的顏色,android:button 則可換成自訂的 drawable,做主題一致的單選控制時用得上。

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