Android learning notes --- get the selected RadioGroup value, androidradiogroup
1. Obtain the RadioGroup control: RadioGroup radioGroup = (RadioGroup) findViewById (R. id. myRadioGroup );
2. Obtain the RadioButton control; RadioButton radioButton = (RadioButton) findViewById (radioGroup. getCheckedRadioButtonId ());
3. Obtain the selected radio value: String text = radioButton. getText (). toString ();
4. Add a listening event to the radioGroup to listen to the internal Event Response of the component: radioGroup. setOnCheckedChangeListener (new RadioGroup. onCheckedChangeListener () {@ Override public void onCheckedChanged (RadioGroup group, int checkedId) {// This function is used to change the value of the selected radioButton, and any operations related to the value. For details, see selectRadioBtn ();}});
5. initialize the above four items in onCreat;
6. Example: protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. waterselect); getViews (); setListenerForView ();}
Private void getViews () {// obtain the radio component information and radioGroup = (RadioGroup) findViewById (R. id. isWaterByContent) of the water in the database );}
Private void setListenerForView () {// select radio selectRadioBtn (); // The listening event radioGroup in the library. 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 );}
From: 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"> <! -- Define the RadioGroup control to select a group for the political outlook --> <RadioGroup android: id = "@ + id/radiogroup1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "vertical"> <! -- Define the RadioButton control and represent the options of Party members --> <RadioButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Party members"/> <! -- Define the RadioButton control to represent the masses --> <RadioButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "masses"/> <! -- Define the RadioButton control to represent the member options --> <RadioButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "member"/> </RadioGroup> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "result" 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 essential package 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; // define the check box object private TextView yl1 for football; // define the result text tag object @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // call the onCreate method of the parent class // set the layout file of the current page to activity_main setContentView (R. layout. activity_main); findView (); // obtain the control setListener () in the page; // set the control listener} private void setListener () {// TODO Auto-generated method stub // sets the listener radiogroup1.setOnCheckedChangeListener (mylistener) for changing the status of all radiogroups;} Radiogroup. onCheckedChangeListener mylistener = new RadioGroup. onCheckedChangeListener () {@ Override public void onCheckedChanged (RadioGroup Group, int Checkid) {// TODO Auto-generated method stub // set the content of TextView to display the CheckBox selection result setText () ;}}; private void findView () {// TODO Auto-generated method stub // obtain the corresponding control object radiogroup1 = (RadioGroup) findViewById (R. id. radiogroup1); yl1 = (TextView) findViewById (R. id. yl1);} private void setText () {String str; yl1.setText (""); // clear the TextView content RadioButton radioButton = (RadioButton) findViewById (callback ()); int id = radiogroup1.getCheckedRadioButtonId (); str = radioButton. getText (). toString (); yl1.setText ("the selected object is: id =" + id + ", value:" + str );}}