Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 小專案:RadioGroup 與 RadioButton 做一個單選測驗題

編輯:Android 開發實例

做過 ASP.NET 的人看到 RadioButtonList,第一個念頭大概是「Android 有對應的東西嗎?」有的。Android 提供 RadioGroup 容器,把多個 RadioButton 收在同一組裡,同一時間只會有一個處於選中狀態,天然適合做單選題。

RadioGroup 的價值在哪

如果不用 RadioGroup,你得自己在每個 RadioButton 的 OnCheckedChanged 裡把其他幾個設為 false,選項一多就是災難。RadioGroup 把這件事收進容器層級處理,你只要監聽群組的變化即可。這裡有個容易搞混的點:

  • 監聽群組:radioGroup.setOnCheckedChangeListener(),回呼帶 checkedId,是單選題該用的方式。
  • 監聽單顆按鈕:radioButton.setOnCheckedChangeListener(),回呼帶 isChecked,適合單一開關式的場景。

版面結構

<RadioGroup
    android:id="@+id/group"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton android:id="@+id/RadioButton01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="選項 A" />
    <RadioButton android:id="@+id/RadioButton02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="選項 B" />
    <RadioButton android:id="@+id/RadioButton03" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="選項 C" />
    <RadioButton android:id="@+id/RadioButton04" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="選項 D" />
</RadioGroup>

核心程式碼

public class TestRadioButton extends Activity {

    private RadioGroup mGroup;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mGroup = (RadioGroup) findViewById(R.id.group);

        mGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.RadioButton03:
                        Show("回答正確,獎勵你 100 分");
                        break;
                    case R.id.RadioButton01:
                    case R.id.RadioButton02:
                    case R.id.RadioButton04:
                        Show("回答錯誤!");
                        break;
                }
            }
        });
    }

    /** 清空作答,讓使用者可以重新挑戰 */
    public void reset() {
        mGroup.clearCheck();
    }

    private void Show(String msg) {
        new AlertDialog.Builder(TestRadioButton.this)
                .setTitle("提示")
                .setMessage(msg)
                .setPositiveButton("確定", null)
                .show();
    }
}

對原寫法做的三處簡化

用 clearCheck() 取代四次 setChecked(false)

原範例在重設按鈕時,逐一檢查四個 RadioButton 再個別設為 false。RadioGroup 提供了 clearCheck(),一行解決,而且選項數量改變時不必同步修改程式。

用 checkedId 直接 switch,省掉 findViewById

原範例先 findViewById(checkedId) 取得 RadioButton,再檢查 btn.isChecked()。但群組回呼觸發時,checkedId 指的那顆必然就是剛被選中的——這正是 RadioGroup 保證「單選」的意義。直接對 checkedId 做 switch 即可。

把正確答案合併處理

三個錯誤選項各自寫一段一模一樣的 Show("回答錯誤!"),用 case 穿透(fall-through)合併成一個分支,程式短了一半,改文案也只需改一處。

幾個實務細節

  • RadioButton 必須是 RadioGroup 的直接子節點,群組才管得到它。若中間隔了一層 LinearLayout,單選效果就會失效。
  • 預設選項可在 XML 對某顆 RadioButton 加 android:checked="true",或在程式裡呼叫 check(R.id.xxx)。
  • 重設時會觸發監聽器。clearCheck() 與 check() 都會引發 onCheckedChanged,若不希望程式性的重設弹出提示對話框,可加一個 boolean isLoading 旗標暫時跳過判斷。
  • 作答後鎖定:想讓使用者答完不能再改,可呼叫 for (int i = 0; i < mGroup.getChildCount(); i++) mGroup.getChildAt(i).setEnabled(false);,重玩時再逐一設回 true。

RadioGroup 搭配 checkedId 的 switch,是 Android 裡處理「多選一」最乾淨的骨架。無論是測驗題、性別選擇還是排序方式,都能沿用同一個模式。

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