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

Android 基礎教學(七):RadioGroup 群組事件的綜合應用

編輯:開發入門

本節要介紹 RadioGroup 的群組事件。RadioGroup 能把彼此不同的 RadioButton 限制在同一個單選按鈕組內,同一組中的按鈕只能做出單一選擇——也就是單選題的效果。

範例的做法是:先設計一個 TextView,再建立一個 RadioGroup 並在其中放入兩個 RadioButton,預設都不勾選。程式執行時以 onCheckedChanged 作為事件觸發點,讓使用者選擇其中一個按鈕,最後把被選中選項的文字顯示在 TextView 上。

效果預覽

RadioGroup 初始畫面

選擇單選項後文字同步更新

string.xml

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

主版面 main.xml

<?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 -->
    <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 -->
        <RadioButton
            android:id="@+id/myRadioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tr_radio_op1"
            />
        <!-- 第二個 RadioButton -->
        <RadioButton
            android:id="@+id/myRadioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tr_radio_op2"
            />
    </RadioGroup>
</LinearLayout>

這裡有個實務上的建議:版面中的寬高盡量改用 fill_parent 與 wrap_content,而不是像範例這樣寫死 px。像素單位在不同解析度的螢幕上會造成版面跑位,改用 dp 與 sp 才能維持一致的可讀性。

主程式 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())
            {
                /* 把 mRadio2 的內容傳到 mTextView1 */
                mTextView1.setText(mRadio2.getText());
            }
        }
    };
}

重點整理

整個範例的關鍵在於監聽器掛在哪一層:事件是掛在 RadioGroup 上,而不是個別的 RadioButton 上。回呼方法 onCheckedChanged 會帶入兩個參數——group 是觸發事件的群組,checkedId 是目前被選中那顆按鈕的 id。用 checkedId 去比對,就能知道使用者選了誰。

這個設計有個好處:新增第三、第四個選項時,你只要在 XML 中補上 RadioButton,再在 if-else 中多加一個分支即可,不必為每顆按鈕個別掛監聽器。

補充兩個常用操作:若想在程式碼中預設勾選某一項,可用 radioGroup.check(R.id.myRadioButton1);若想取得目前選項的文字,則用 findViewById(group.getCheckedRadioButtonId()) 拿到那顆按鈕再取 getText()。另外別忘了,RadioGroup 的「只能選一個」是群組層級的保證——如果兩個 RadioButton 不在同一個 RadioGroup 裡,它們是可以同時被勾選的。

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