編輯:Android 開發實例
這一節來看 RadioGroup 的群組事件。RadioGroup 能把多個 RadioButton 限縮在同一個按鈕組內,同一組裡永遠只能選一個——也就是單選題的行為。
我們要做的是:畫面上放一個 TextView 與一個含兩個 RadioButton 的 RadioGroup,預設都不選;使用者點選其中一項時,透過 onCheckedChanged 觸發事件,把被選中的選項文字顯示到 TextView 上。
[效果圖]
先把要顯示的文字抽出來放進資源檔,這是 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>
<?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 才是正確習慣。
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" 就行。
Android 多語言支援入門:values 資料夾與 strings.xml 的組織方式
一個上架到 Google Play 的 App,可能同時被德國、日本、巴西的使用者安裝。如果畫面上的字永遠只有英文,對不懂英文的使用者來說,這個 App 等於不存在。本地
Android 多媒體播放:MediaPlayer 從零做出音樂播放器
Android 提供多種播放音訊與影片的方式,而最直覺、也最常被拿出來教學的,莫過於 MediaPlayer。它把解碼與播放細節藏在後面,讓你專心寫控制邏輯。 取得實例
Android 開發實戰:整合百度地圖 SDK 完整指南
百度地圖 Android SDK 讓開發者能夠輕鬆存取百度地圖服務與資料,打造功能豐富、互動性強的 LBS(位置服務)應用。它提供地圖顯示、定位、搜尋等一整套能力,且所有
Android 實作:讓圖片同時支援平移、縮放與旋轉
前言先前因為專案需求,用到了圖片的點擊顯示/取消、平移與縮放功能。後來突然想再加上旋轉,上網看了不少相關範例,卻始終找不到能同時滿足我所有需求的實作,於是乾脆自己動手做了