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

Android 控件開發:RadioButton 單選與 CheckBox 多選的實作

編輯:關於 Android 程式設計

表單類需求裡,單選與多選是最基礎的兩種互動。Android 提供了 RadioGroup+RadioButton 的組合來處理「只能選一個」,以及 CheckBox 來處理「可以選多個」。本文用一個性別選擇+興趣多選的小範例把兩者講清楚。

一、單選:RadioGroup 搭配 RadioButton

要點是:單選的互斥性由 RadioGroup 保證,而不是 RadioButton 自己。同一個 RadioGroup 內的多個 RadioButton 會自動互斥,不同 RadioGroup 之間則互不影響。監聽器建議掛在 RadioGroup 上(setOnCheckedChangeListener),而不是每個 RadioButton 各掛一個,這樣程式碼乾淨得多。

二、多選:CheckBox

CheckBox 天生就是獨立的,彼此不影響。每個 CheckBox 各自實作 CompoundButton.OnCheckedChangeListener,在 onCheckedChanged 的 isChecked 參數中就能知道當前狀態。

三、Activity 程式碼

package com.example.radiotest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {

    private RadioGroup generGroup = null;
    private RadioButton maleButton = null;
    private RadioButton femaleButton = null;

    private CheckBox swin = null, run = null, jump = null;

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

        generGroup = (RadioGroup) findViewById(R.id.mySexGroup);
        maleButton = (RadioButton) findViewById(R.id.maleButton);
        femaleButton = (RadioButton) findViewById(R.id.femaleButton);

        swin = (CheckBox) findViewById(R.id.swinBox);
        run = (CheckBox) findViewById(R.id.runBox);
        jump = (CheckBox) findViewById(R.id.jumpBox);

        // RadioGroup 的監聽器:一次處理整組的變化
        generGroup.setOnCheckedChangeListener(
                new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == maleButton.getId()) {
                    Toast.makeText(MainActivity.this, R.string.male,
                            Toast.LENGTH_SHORT).show();
                } else if (checkedId == femaleButton.getId()) {
                    Toast.makeText(MainActivity.this, R.string.female,
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        // CheckBox 各自監聽
        swin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println("swin is " + (isChecked ? "checked!" : "unchecked!"));
            }
        });

        run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println("run is " + (isChecked ? "checked!" : "unchecked!"));
            }
        });

        jump.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println("jump is " + (isChecked ? "checked!" : "unchecked!"));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

四、布局檔

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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

        <RadioButton
            android:id="@+id/maleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male" />

        <RadioButton
            android:id="@+id/femaleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female" />
    </RadioGroup>

    <CheckBox
        android:id="@+id/swinBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/swin" />

    <CheckBox
        android:id="@+id/runBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/run" />

    <CheckBox
        android:id="@+id/jumpBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/jump" />
</LinearLayout>

五、實務提醒

不要用文字當 Toast 參數。範例原本寫成 Toast.makeText(this, male, ...) 是編譯不過的,第二個參數需要是 CharSequence 或資源 ID,字串資源請寫成 R.string.male。

監聽器要小心回收。在 ListView 的 convertView 中給 CheckBox 掛監聽器時,記得先把監聽器設為 null 再呼叫 setChecked,否則會觸發不必要的回呼並造成狀態錯亂。

預設選中。布局中加上 android:checked="true" 即可預設勾選;若用程式設定,同樣要注意監聽器時機。

樣式客製。系統預設的單選/多選圖示可以用 android:button 或 android:drawableLeft 換成自訂的 selector,讓表單風格與整體設計一致。

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