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

Android 基礎控制項:RadioButton 與 CheckBox 用法

編輯:關於 Android 程式設計

單選按鈕(RadioButton)與核選框(CheckBox)都是提供使用者快速選擇的良好體驗:在不需要使用者親手輸入的場合,這類控制項能讓操作更為便捷。兩者的差別在於——RadioButton 在一組選項中只能選擇其中一個,而 CheckBox 則可以勾選多個。以下以 RadioButton 為例示範。

示例:RadioButton 的用法

先在版面配置檔中定義一組單選按鈕,分為三個選項:

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

    <!-- 定義一組 RadioButton,分為三個選項 -->
    <RadioButton
        android:id="@+id/first_radiobutton"
        android:checked="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAction"
        android:text="男" />

    <RadioButton
        android:id="@+id/second_radiobutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAction"
        android:text="女" />

    <RadioButton
        android:id="@+id/third_radiobutton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onAction"
        android:text="其他" />
</LinearLayout>

其中 android:checked="true" 用來設定預設被勾選的選項;android:onClick="onAction" 則是設定點擊事件的監聽方法,實際的行為在 Java 程式碼中實作。若要將多個按鈕歸為同一組,可使用 RadioGroup 來定義。

Java 程式碼

package xbb.bzq.android.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;

public class RadioButtonAndCheckBoxTestActivity extends Activity {
    // 定義三個 RadioButton 變數
    private RadioButton mButton1, mButton2, mButton3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // 實例化三個單選按鈕控制項
        mButton1 = (RadioButton) findViewById(R.id.first_radiobutton);
        mButton2 = (RadioButton) findViewById(R.id.second_radiobutton);
        mButton3 = (RadioButton) findViewById(R.id.third_radiobutton);
    }

    /**
     * 這是實作的監聽方法,主要用來切換選項的勾選狀態
     * @param v
     */
    public void onAction(View v) {
        // 透過取得 id 來判斷使用者的選擇,再改變控制項的勾選狀態
        switch (v.getId()) {
            case R.id.first_radiobutton:
                mButton2.setChecked(false);
                mButton3.setChecked(false);
                mButton1.setChecked(true);
                break;
            case R.id.second_radiobutton:
                mButton1.setChecked(false);
                mButton3.setChecked(false);
                mButton2.setChecked(true);
                break;
            case R.id.third_radiobutton:
                mButton2.setChecked(false);
                mButton1.setChecked(false);
                mButton3.setChecked(true);
                break;
        }
    }
}

透過 onAction 方法判斷被點擊的選項,並將其餘選項設為未勾選、將目前選項設為勾選,即可實現單選的互斥效果。

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