編輯:關於 Android 程式設計
RadioGroup 與 RadioButton 是 Android 中實作單選控制的標準組合。它的運作模式與多數 UI 框架類似:把幾個 RadioButton 放進同一個 RadioGroup,群組會自動保證「最多只有一個被選中」,並透過 OnCheckedChangeListener 通知你哪一個被選了。
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class Radio extends Activity {
private TextView myTextView;
private RadioButton chinaBtn;
private RadioButton ukBtn;
private RadioButton usaBtn;
private RadioGroup rg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 以 ID 找出各元件
myTextView = (TextView) findViewById(R.id.myTextView);
chinaBtn = (RadioButton) findViewById(R.id.china_Button);
ukBtn = (RadioButton) findViewById(R.id.uk_Button);
usaBtn = (RadioButton) findViewById(R.id.usa_Button);
rg = (RadioGroup) findViewById(R.id.rBtnGroup);
// 監聽要設在 RadioGroup 上,而不是個別的 RadioButton
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.china_Button == checkedId) {
myTextView.setText("您選擇的國家是:" + chinaBtn.getText().toString());
} else if (R.id.uk_Button == checkedId) {
myTextView.setText("您選擇的國家是:" + ukBtn.getText().toString());
} else if (R.id.usa_Button == checkedId) {
myTextView.setText("您選擇的國家是:" + usaBtn.getText().toString());
}
}
});
}
}
<?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="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
<RadioGroup
android:id="@+id/rBtnGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/china_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中國" />
<RadioButton
android:id="@+id/uk_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英國" />
<RadioButton
android:id="@+id/usa_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="美國" />
</RadioGroup>
</LinearLayout>

這是最容易搞錯的地方。RadioButton 本身繼承自 CompoundButton,也有 setOnCheckedChangeListener,但在 RadioGroup 裡應該監聽群組。原因是:當使用者從 A 切到 B 時,A 會先觸發「取消勾選」,若你監聽的是個別按鈕,就會收到兩次回呼,狀態判斷變得很麻煩。監聽群組則只會給你「最終被選中的那一個」。
上例用 if-else 逐一比對 ID,這是教學寫法。實務上更乾淨的方式是先把選項與資料綁在一起:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.china_Button: selectedCountry = "CN"; break;
case R.id.uk_Button: selectedCountry = "UK"; break;
case R.id.usa_Button: selectedCountry = "US"; break;
}
myTextView.setText("您選擇的國家是:" + group.findViewById(checkedId).getTag());
}
});
要預設勾選某一項,可以在 XML 中對該 RadioButton 加上 android:checked="true",或在程式碼中呼叫 rg.check(R.id.uk_Button)。
若要動態產生選項,直接 new 出 RadioButton 並 rg.addView() 即可,群組的互斥邏輯仍然有效:
RadioButton rb = new RadioButton(this);
rb.setId(View.generateViewId());
rb.setText("新選項");
rg.addView(rb);
「只能選一個」用 RadioGroup;「可以多選」應該用 CheckBox 或 CheckedTextView。RadioGroup 只能容納 RadioButton 作為直接子元件,若你塞進其他 View,群組的互斥行為就不保證了。
另外提醒,RadioButton 的 buttonTint(API 21 以上可用 android:buttonTint)能改勾選圓點的顏色,android:button 則可換成自訂的 drawable,做主題一致的單選控制時用得上。
自定義衛星式菜單(弧形菜單)完整實作教學
衛星式菜單又叫弧形菜單:主按鈕固定在角落,點一下,子菜單項沿著一段圓弧依序散開,同時自轉進場。要做出來,主要得處理兩件事——動畫,以及一個自定義的 ViewGroup。
OpenCV 使用 Harris 演算法實作角點檢測
效果展示 下面先觀察 Harris 角點檢測在圖像上的實際輸出。角點通常會以圓點標記,越明顯的交會區域,響應值越高。 什麼是 Harris 角點檢測 角點可以理解為兩
Android 圖片裁剪功能的完整實作
圖片裁剪是常見需求。系統提供了隱式 Intent 可以調起相冊裁剪,但各家 ROM 實現差異極大,同一段代碼在不同機型上表現各異,個別「奇葩」機型甚至直接崩潰或返回空結果
Android 俄羅斯方塊遊戲完整示例
以下畫面展示 Android 俄羅斯方塊遊戲的執行效果,包含遊戲主介面、方塊堆疊、操作區域與狀態顯示。這類遊戲常用自訂 View 或 Canvas 繪製方塊,並透過計時器