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

Android 單選鈕與多選鈕(RadioButton 與 CheckBox)的使用教學

編輯:關於 Android 程式設計

單選鈕(RadioButton)與多選鈕(CheckBox)是 Android 表單設計中不可或缺的元件。前者用於「多選一」的情境(如性別選擇),後者則用於「可複選」的情境(如興趣標記)。以下透過完整範例說明它們的用法。

一、版面配置檔

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/radioGroup">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radio1"
        android:text="@string/female"/>
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radio2"
        android:text="@string/male"/>
</RadioGroup>

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

二、Activity 程式碼

package com.example.android1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class LinearLayOut extends Activity {
    private RadioGroup radioGroup;
    private RadioButton radio1, radio2;
    private CheckBox runBox, singBox, danceBox;

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

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new MyButtonListener());  // 綁定監聽器

        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        runBox = (CheckBox) findViewById(R.id.runBox);
        singBox = (CheckBox) findViewById(R.id.singBox);
        danceBox = (CheckBox) findViewById(R.id.danceBox);

        // 單選鈕監聽器(監聽整個 RadioGroup)
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (radio1.getId() == checkedId) {
                    Toast.makeText(LinearLayOut.this, "選擇了女", Toast.LENGTH_SHORT).show();
                } else if (radio2.getId() == checkedId) {
                    System.out.println("男");
                }
            }
        });

        // 複選框監聽器:每個 CheckBox 都需要獨立設定
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println(isChecked ? "runBox is selected" : "runBox is unselected");
            }
        });

        singBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println(isChecked ? "singBox is selected" : "singBox is unselected");
            }
        });

        danceBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println(isChecked ? "danceBox is selected" : "danceBox is unselected");
            }
        });
    }

    class MyButtonListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setClass(LinearLayOut.this, TableLayout.class);
            LinearLayOut.this.startActivity(intent);
        }
    }
}

關鍵觀念

  • 單選鈕:必須放在 RadioGroup 中,並為整個群組設定 OnCheckedChangeListener,透過比對 checkedId 判斷被選中的選項。
  • 多選鈕:每個 CheckBox 都是獨立的,需要各自設定 CompoundButton.OnCheckedChangeListener,並透過 isChecked 參數判斷勾選狀態。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved