The Radiogroup component contains several RadioButton components, one for each RadioButton, and the Radiogroup is similar to making a single choice.
Radiogroup can be understood as a container for storing RadioButton, he organizes multiple RadioButton together to form a group, and the user can only select a certain RadioButton in the group, So the object that the user directly operates is the RadioButton component.
The following example is to first select an option, press the Submit button, and a message box will pop up prompting you to select the information.
The first is the layout file:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:id= "@+id/linearlayout1" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:orientation= "vertical" tools:context= "main.test_ Radiogroup. Mainactivity " > <TextView Android:id= "@+id/textview1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/TV1" /> <RadioGroup android:id= "@+id/radiogroup1" android:layouT_width= "Wrap_content" android:layout_height= "Wrap_content" > <RadioButton android:id= "@+id/radio0" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:checked= "true" android:text= "@string/rb1" /> < Radiobutton android:id= "@+id/radio1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/rb2" /> <radiobutton android:id= "@+id/ Radio2 " android:layout_width=" Wrap_ Content " android:layout_height=" Wrap_ Content " android:text=" @string/rb3 " /> <RadioButton android:id= "@+id/radio3" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/rb4"/> <radiobutton android:id= "@+id/radio4" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/rb5"/> </RadioGroup> <Button Android:id= "@+id/button1" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/btn" /></LinearLayout>
Next is the Strings.xml file:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >test_radiogroup</ string> <string name= "action_settings" >Settings</string> <string name= "TV1" > Please choose your favorite dish: </ string> <string name= "RB1" > Wok egg </string> <string name= "rb2" > Honey Juice two </string> <string na Me= "Rb3" > Porcelain altar lamb </string> <string name= "RB4" > Shredded pork with bottom </string> <string name= "rb5" > Live fish Live Eat </ string> <string name= "BTN" > Submit </string></resources>
Again is the Android source file:
package main.test_radiogroup;import android.support.v7.app.actionbaractivity;import android.view.view;import android.view.view.onclicklistener;import android.widget.button;import Android.widget.radiobutton;import android.widget.toast;import android.os.bundle;public class MainActivity extends ActionBarActivity implements OnClickListener{ private RadioButton rbtn1=null,rbtn2=null,rbtn3=null,rbtn4=null,rbtn5=null; private Button btn1=null; @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); rbtn1= (RadioButton) Findviewbyid (r.id.radio0); rbtn2= (RadioButton) Findviewbyid (R.ID.RADIO1); rbtn3= (RadioButton) findViewById ( R.id.radio2); rbtn4= (RadioButton) Findviewbyid (R.id.radio3); rbtn5= (RadioButton) Findviewbyid (R.id.radio4); btn1= (Button) findViewById ( R.id.button1); Btn1.setonclicklistener (mainactivity.this); } @Override public void onclick (view arg0) { string str= ""; if (rbtn1.isChecked ()) str= Rbtn1.gettext (). toString (); else if (rbtn2.ischecked ()) str=rbtn2.gettext (). toString (); else if (rbtn3.ischecked ()) str=rbtn3.getText (). ToString (); else if (rbtn4.ischecked ()) str= Rbtn4.gettext (). toString (); else if (rbtn5.ischecked ()) str=rbtn5.gettext (). toString (); else { } toast.maketext (Mainactivity.this, "You have chosen" +str,Toast.LENGTH_LONG ). Show ();//pop-up Select message box }}
Finally, the test results are as follows:
Radiogroup of common Android components