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

RadioGroup 單選組實戰:組監聽與選項文字回顯

編輯:關於 Android 程式設計

RadioGroup 的作用是把若干 RadioButton 綁成一個互斥組:同一組內永遠只有一個能選中,行為等同單選題。本文用一個小例子把它的完整用法走一遍。

需求拆解

畫面上放一個 TextView 作為問題提示,一個包含兩個 RadioButton 的 RadioGroup 作為選項,初始狀態都不選。當使用者點選其中一項時,透過 onCheckedChanged 觸發回應,把被選中按鈕的文字顯示到 TextView 上。

先看運行結果:

一、字符串資源

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, RadioGroupDemo</string>
    <string name="app_name">RadioGroupDemo</string>
    <string name="tr_radio_op1">帥哥</string>
    <string name="tr_radio_op2">美女</string>
    <string name="str_radio_question1">請問你是?</string>
</resources>

二、主佈局

把 RadioButton 直接嵌在 RadioGroup 標籤內,層級淺、結構清晰:

 <?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="228px"
        android:layout_height="49px"
        android:text="@string/str_radio_question1"
        android:textSize="30sp" />

    <!-- 建立一個 RadioGroup -->
    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="137px"
        android:layout_height="216px"
        android:orientation="vertical" >

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

        <RadioButton
            android:id="@+id/myRadioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tr_radio_op2" />
    </RadioGroup>
</LinearLayout>

三、控制層程式碼

監聽器註冊在 RadioGroup 而非單個按鈕上,這是很多人第一次寫會搞錯的地方——註冊在組上才能拿到「當前選中了誰」。

package com.android.test;

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

public class RadioGroupDemo extends Activity {
    public TextView mTextView1;
    public RadioGroup mRadioGroup1;
    public RadioButton mRadio1, mRadio2;

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

        /* 取得 TextView、RadioGroup、RadioButton 對象 */
        mTextView1 = (TextView) findViewById(R.id.myTextView);
        mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
        mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
        mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);

        /* RadioGroup 用 OnCheckedChangeListener 來運行 */
        mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);
    }

    private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId == mRadio1.getId()) {
                /* 把 mRadio1 的內容傳到 mTextView1 */
                mTextView1.setText(mRadio1.getText());
            } else if (checkedId == mRadio2.getId()) {
                /* 把 mRadio2 的內容傳到 mTextView1 */
                mTextView1.setText(mRadio2.getText());
            }
        }
    };
}

小結與延伸

三個要點記牢:監聽掛在 RadioGroup 上、回呼參數 checkedId 就是被選中項的 id、取消全部選擇可用 check(-1)。實務中若選項較多,也可以改用 RadioGroup 搭配 RadioButton 的 buttonTint 統一配色,或直接把 setOnCheckedChangeListener 換成表單式驗證邏輯,讓單選組承擔更完整的輸入職責。

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