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

Android 入門教程(十一):RadioGroup 單選按鈕組的綜合應用

編輯:Android 開發實例

這一節來看 RadioGroup 的群組事件。RadioGroup 能把多個 RadioButton 限縮在同一個按鈕組內,同一組裡永遠只能選一個——也就是單選題的行為。

我們要做的是:畫面上放一個 TextView 與一個含兩個 RadioButton 的 RadioGroup,預設都不選;使用者點選其中一項時,透過 onCheckedChanged 觸發事件,把被選中的選項文字顯示到 TextView 上。

[效果圖]

一、字串資源 string.xml

先把要顯示的文字抽出來放進資源檔,這是 Android 的建議做法,後續要做多語系會輕 jmpp多。

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

二、版面檔 main.xml

<?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">

    <TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_radio_question1"
        android:textSize="30sp" />

    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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>

這裡有個小改進:原範例把寬高寫成 228px、137px 這類絕對像素,換到不同解析度的裝置上就會跑位。改用 wrap_content 才是正確習慣。

三、主程式 RadioGroupDemo.java

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()) {
                mTextView1.setText(mRadio2.getText());
            }
        }
    };
}

補充

監聽器掛在 RadioGroup 而不是個別的 RadioButton 上,這是關鍵:這樣不論將來增加多少個選項,同一個回呼都能處理,不必為每顆按鈕各寫一份。

若想知道「目前選了哪一個」,用 mRadioGroup1.getCheckedRadioButtonId() 即可拿到 id,沒有選任何項時會回傳 -1;要預設勾選某一項,在 XML 上對該 RadioButton 加上 android:checked="true" 就行。

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