Android 學習筆記---擷取RadioGroup的選定值,androidradiogroup
1,擷取RadioGroup控制項: RadioGroup radioGroup = (RadioGroup)findViewById(R.id.myRadioGroup);
2,擷取RadioButton控制項; RadioButton radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
3,擷取選中的radio的值: String text = radioButton.getText().toString();
4,為radioGroup添加監聽事件,用來監聽組件內部的事件響應: radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //在這個函數裡面用來改變選擇的radioButton的數值,以及與其值相關的 //任何操作,詳見下文 selectRadioBtn(); } });
5,在onCreat中需要初始化上面的四條資訊;
6,整體的使用範例: protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.waterselect); getViews(); setListenerForView(); }
private void getViews(){ //擷取庫內上水的radio組件資訊、 radioGroup = (RadioGroup)findViewById(R.id.isWaterByContent); }
private void setListenerForView(){ //選擇radio selectRadioBtn(); //庫內上水的監聽事件 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { selectRadioBtn(); } }); }
private void selectRadioBtn(){ radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()); waterIn = radioButton.getText().toString(); Log.i("radio", waterIn); }
來自:http://blog.sina.com.cn/s/blog_9c5364110101c1bj.html
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <!-- 定義RadioGroup控制項 ,代表政治面貌選擇組 --> <RadioGroup android:id="@+id/radiogroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <!-- 定義RadioButton控制項 ,代表黨員選項 --> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="黨員" /> <!-- 定義RadioButton控制項 ,代表群眾選項 --> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="群眾" /> <!-- 定義RadioButton控制項 ,代表團員選項 --> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="團員" /> </RadioGroup> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="結果" android:id="@+id/yl1"></TextView> <!--android:layout_gravity="center_horizontal" />--></LinearLayout>
package com.example.yanlei.yl2;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;//匯入必備的包import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.CompoundButton.OnCheckedChangeListener;public class MainActivity extends AppCompatActivity { private RadioGroup radiogroup1; //定義足球的複選框對象 private TextView yl1; //定義結果文本便簽對象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //調用父類的onCreate方法 //通過setContentView方法設定當前頁面的布局檔案為activity_main setContentView(R.layout.activity_main); findView(); //擷取頁面中的控制項 setListener(); //設定控制項的監聽器 } private void setListener() { // TODO Auto-generated method stub //設定所有Radiogroup的狀態改變監聽器 radiogroup1.setOnCheckedChangeListener(mylistener); } RadioGroup.OnCheckedChangeListener mylistener=new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup Group, int Checkid) { // TODO Auto-generated method stub //設定TextView的內容顯示CheckBox的選擇結果 setText(); } }; private void findView() { // TODO Auto-generated method stub //通過findViewById得到對應的控制項對象 radiogroup1 = (RadioGroup)findViewById(R.id.radiogroup1); yl1 = (TextView)findViewById(R.id.yl1); } private void setText(){ String str; yl1.setText(""); //清空TextView的內容 RadioButton radioButton = (RadioButton)findViewById(radiogroup1.getCheckedRadioButtonId()); int id= radiogroup1.getCheckedRadioButtonId(); str=radioButton.getText().toString(); yl1.setText("選擇對象是:id="+id+",值:"+str); }}