編輯:開發入門
本節要介紹 RadioGroup 的群組事件。RadioGroup 能把彼此不同的 RadioButton 限制在同一個單選按鈕組內,同一組中的按鈕只能做出單一選擇——也就是單選題的效果。
範例的做法是:先設計一個 TextView,再建立一個 RadioGroup 並在其中放入兩個 RadioButton,預設都不勾選。程式執行時以 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>
<?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 才能維持一致的可讀性。
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 裡,它們是可以同時被勾選的。
Android 簡訊程式實作:TinySMS 與 SmsManager 傳送機制
以 TinySMS 理解簡訊功能 簡訊是手機最基礎的應用之一。本文以 TinySMS 為例,說明如何建立一個簡易的 Android 簡訊介面,並使用 SmsManager
用 Eclipse 開發 Android 應用程式(三):版面配置、清單檔與模擬器除錯
main.xml 這個範例應用包含一個活動與一個視圖。應用裡有個名為 main.xml 的檔案,代表活動主 UI 的視覺層面。請注意,main.xml 裡並未指定這個版面
用 Android、Scala 與 Eclipse 打造行動應用:以單位轉換器為例
先決條件 本文將帶你打造一個運行於 Android 裝置上的行動應用。你需要安裝 Android SDK,本文採用 V1.5 SDK。應用程式碼將以 Scala 語言撰寫
Android Activity 的生命周期結構
Activity 是 Android 應用中最常見的元件之一。它的生命週期從建立、啟動、暫停、停止到銷毀,各階段都有對應的函式。理解這些函式,有助於開發更穩定的應用程式。