API Demos 2.3 學習筆記 (15)-- Views->Radio Group
更多精彩內容,請點擊閱讀:《API Demos 2.3 學習筆記》
RadioGroup的創建主要有兩種方法:
1、在xml布局文件中
<RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:checkedButton="@+id/lunch" android:> <RadioButton android:text="@string/radio_group_1_breakfast" android: /> <RadioButton android:text="@string/radio_group_1_lunch" android: /> <RadioButton android:text="@string/radio_group_1_dinner" android: /> <RadioButton android:text="@string/radio_group_1_all" android: /> </RadioGroup>注:上麵演示的是在一個RadioGroup中包含四個RadioButton的情況,您可以根據自己實際需要來增加或者減少RadioButton的個數。
2、在Java代碼中
// 通過findViewById方法獲得一個RadioGroup對象 RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.menu); // 向RadioGroup中動態添加一個RadioButton對象 RadioButton newRadioButton = new RadioButton(this); newRadioButton.setText(R.string.radio_group_snack); newRadioButton.setId(R.id.snack); LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); mRadioGroup.addView(newRadioButton, 0, layoutParams);注:這裏我們首先創建了一個 RadioGroup對象和一個 RadioButton對象,然後通過 addView函數將 RadioButton添加到RadioGroup中。其中 addView函數的第二個參數,表示將 RadioButton添加到RadioGroup內部的什麼位置(從0開始)。 RadioButton通過 setText和 setId分別設定文本和資源ID號碼。
當我們做單項選擇題的時候,可以很快看到選擇的是哪個選項。但是在Android中使用RadioGroup時,我們怎麼通過程序來獲得用戶到底選擇的是哪個選項呢?這就要用到我們常用的方法(監聽器)了。具體使用方法如下:
// 通過findViewById方法獲得一個RadioGroup對象 RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.menu); //當RadioButton狀態發生改變時,觸發監聽器,執行下麵的動作。當清除選擇項時,checkedId為-1。 mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub String choice = null; //根據checkedId判斷用戶選擇了哪一個選項,並執行相應的動作。 switch (checkedId) { case R.id.breakfast: choice = "breakfast"; break; case R.id.lunch: choice = "lunch"; break; case R.id.dinner: choice = "dinner"; break; case R.id.all: choice = "all"; break; case R.id.snack: choice = "snack"; break; default: break; } } });注:我們可以根據監聽器中的 checkedId來判斷是哪一個選項被選擇,並執行相應的動作。
下麵我們進行實例代碼解析:
res-values-string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="radio_group_1_breakfast">Breakfast</string> <string name="radio_group_1_lunch">Lunch</string> <string name="radio_group_1_dinner">Dinner</string> <string name="radio_group_1_all">All of them</string> <string name="radio_group_1_selection">You have selected: (none)</string> <string name="radio_group_1_clear">Clear</string> </resources>
res-layout-radio_group_1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 一個RadioGroup(單項選擇組)對象,內含4個RadioButton(單項選擇按鈕)和一個TextView對象 --> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:checkedButton="@+id/lunch" android:> <RadioButton android:text="@string/radio_group_1_breakfast" android: /> <RadioButton android:text="@string/radio_group_1_lunch" android: /> <RadioButton android:text="@string/radio_group_1_dinner" android: /> <RadioButton android:text="@string/radio_group_1_all" android: /> <TextView android:text="@string/radio_group_1_selection" android: /> </RadioGroup> <!-- 一個Button對象,用於清除RadioGroup的選擇項 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio_group_1_clear" android: /> </LinearLayout>
src-com.example.android.apis.view-RadioGroup1.java
package com.example.android.apis.view; import com.example.android.apis.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.RadioGroup; import android.widget.Button; import android.widget.RadioButton; import android.widget.LinearLayout; public class RadioGroup1 extends Activity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { private TextView mChoice; private RadioGroup mRadioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio_group_1); // 通過findViewById方法獲得一個RadioGroup對象 mRadioGroup = (RadioGroup) findViewById(R.id.menu); // 向RadioGroup中動態添加一個RadioButton對象 RadioButton newRadioButton = new RadioButton(this); newRadioButton.setText(R.string.radio_group_snack); newRadioButton.setId(R.id.snack); LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams( RadioGroup.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT); mRadioGroup.addView(newRadioButton, 0, layoutParams); //為RadioGroup添加監聽器,當點擊RadioButton時,會觸發監聽器, 執行onCheckedChanged中的動作 mRadioGroup.setOnCheckedChangeListener(this); //在TextView控件上顯示被選擇項的提示信息 String selection = getString(R.string.radio_group_selection); mChoice = (TextView) findViewById(R.id.choice); mChoice.setText(selection + mRadioGroup.getCheckedRadioButtonId()); // 通過findViewById方法獲得一個Button對象,點擊該對象會清除RadioGroup中的選擇項 Button clearButton = (Button) findViewById(R.id.clear); clearButton.setOnClickListener(this); } //當RadioButton狀態發生改變時,觸發監聽器,執行下麵的動作。當清除選擇項時,checkedId為-1。 public void onCheckedChanged(RadioGroup group, int checkedId) { String selection = getString(R.string.radio_group_selection); String none = getString(R.string.radio_group_none); String choice = null; //根據checkedId判斷用戶選擇了哪一個選項,並執行相應的動作。 switch (checkedId) { case R.id.breakfast: choice = "breakfast"; break; case R.id.lunch: choice = "lunch"; break; case R.id.dinner: choice = "dinner"; break; case R.id.all: choice = "all"; break; case R.id.snack: choice = "snack"; break; default: break; } mChoice.setText(selection + choice + (checkedId == View.NO_ID ? none : checkedId)); } //點擊clearButton時,清空所有RadioButton的選擇狀態 public void onClick(View v) { mRadioGroup.clearCheck(); } }
最後更新:2017-04-02 06:52:01