1, Get Radiogroup control: Radiogroup radiogroup = (radiogroup) Findviewbyid (R.id.myradiogroup);
2, get RadioButton control; RadioButton RadioButton = (RadioButton) Findviewbyid (Radiogroup.getcheckedradiobuttonid ());
3, gets the value of the selected radio: String text = Radiobutton.gettext (). toString ();
4, add a listener event for Radiogroup to listen for event response inside the component: Radiogroup.setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener ( {@Override public void oncheckedchanged (radiogroup group, int checkedid) {//In this function is used to change the value of the selected RadioButton and/or any Please refer to the following selectradiobtn (); } });
5, in the oncreat need to initialize the above four information;
6, the overall use of the sample: protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.waterselect); Getviews (); Setlistenerforview (); }
private void Getviews () {//Get Radio component information for water inside the library, Radiogroup = (radiogroup) Findviewbyid (r.id.iswaterbycontent); }
private void Setlistenerforview () {//Select Radio Selectradiobtn (); Monitoring Event Radiogroup.setoncheckedchangelistener (new Radiogroup.oncheckedchangelistener () {@Override public void for Sheung Shui in the vault 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
<LinearLayoutxmlns: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" > <!--defining Radiogroup controls, representing the political face selection group - <RadiogroupAndroid:id= "@+id/radiogroup1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical" > <!--defining RadioButton controls, representing party member options - <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Party Member" /> <!--defines the RadioButton control, which represents the mass option - <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "The masses" /> <!--define RadioButton controls, delegation options - <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "League Member" /> </Radiogroup> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Result"Android:id= "@+id/yl1"></TextView> <!--android:layout_gravity= "Center_horizontal"/> -</LinearLayout>
Packagecom.example.yanlei.yl2;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;//Import the required packagesImportandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.RadioButton;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.TextView;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener; Public classMainactivityextendsappcompatactivity {PrivateRadiogroup Radiogroup1;//Define a check box object for a football PrivateTextView Yl1;//Define a result text note Object@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//call the OnCreate method of the parent class//set the current page's layout file to Activity_main by using the Setcontentview methodSetcontentview (R.layout.activity_main); Findview (); //getting controls in a pageSetlistener ();//to set the listener for a control } Private voidSetlistener () {//TODO auto-generated Method Stub//Set the status change listener for all RadiogroupRadiogroup1.setoncheckedchangelistener (MyListener); } Radiogroup.oncheckedchangelistener MyListener=NewRadiogroup.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Radiogroup Group,intcheckid) { //TODO auto-generated Method Stub//Setting the contents of the TextView displays the selection result of the checkboxSetText (); } }; Private voidFindview () {//TODO auto-generated Method Stub//get the corresponding control object by FindviewbyidRadiogroup1 =(Radiogroup) Findviewbyid (r.id.radiogroup1); YL1=(TextView) Findviewbyid (R.ID.YL1); } Private voidSetText () {String str; Yl1.settext ("");//Empty the contents of TextViewRadioButton RadioButton =(RadioButton) Findviewbyid (Radiogroup1.getcheckedradiobuttonid ()); intId=Radiogroup1.getcheckedradiobuttonid (); STR=Radiobutton.gettext (). toString (); Yl1.settext ("Select object is: id=" +id+ ", Value:" +str); }}
Android Learning Notes---Get selected values for Radiogroup