編輯:關於 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 換成表單式驗證邏輯,讓單選組承擔更完整的輸入職責。
Android 系統開發:Linux 進程核心概念入門
做 Android 系統層開發,遲早要面對 Linux 的進程模型。本文梳理幾個最基礎也最關鍵的概念:proc 虛擬文件系統、虛擬內存地址空間、進程的運行機制與生命週期。
Android 數據存儲之 SQLite:從 SQL 語句到完整通訊錄案例
SQLite 是一款數據庫,與 Oracle、SQLServer、MySQL 同屬關系型數據庫。Android 系統自帶了這個輕量級的 SQLite。一、特點輕量級:只有
ListView 復用導致選項狀態錯亂的原因與解決方案
需求是這樣的: 每道題有 A、B、C、D 四個選項。點擊某個選項後,如果它是正確答案就顯示對勾圖標,否則顯示叉號圖標。 第一版代碼寫得很直觀:點擊時比較所選項與正確答案
Android 事件分發機制全解析:從 dispatchTouchEvent 到 requestDisallowIntercept
一、概述 說到 Android 的事件分發機制,真是既熟悉又陌生。每次需要時翻翻源碼,總能若有所得;可要憑自己的理解從頭到尾講一遍,卻又一句也說不上來。歸根究底,大概是自