One: RadioButton related properties:
1.Activity
//radio Button Public classRadiobuttonactivityextendsActivity {Privatecontext Context; PrivateRadiogroup Sexradiogroup; PrivateRadioButton Maleradiobutton; PrivateRadioButton Femaleradiobutton; PrivateRadioButton Sexradiobutton; PrivateButton Submitbutton; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.radio_button); Init (); Addaction (); } Private voidinit () {Context= This; Sexradiogroup=(Radiogroup) Findviewbyid (r.id.sexradiogroupid); Maleradiobutton=(RadioButton) Findviewbyid (R.id.maleradiobuttonid); Femaleradiobutton=(RadioButton) Findviewbyid (R.id.femaleradiobuttonid); Sexradiobutton=(RadioButton) Findviewbyid (R.id.sexradiobuttonid); Submitbutton=(Button) Findviewbyid (R.id.submitbuttonid); } Private voidAddaction () {//Sexradiogroup.setonclicklistener (L), Setonclicklistener: trigger when clicked//Setoncheckedchangelistener: Triggers when the state changes//both can realize the monitoring of the state change of checkbox, but in general, more is Setoncheckedchangelistener. //because, when the status of the checkbox is not changed by the click event, but by other means, such as SetCheck (), Setonclicklistener cannot complete the monitoring in this case. //Oncheckchangedlistener Monitor the status of the checkbox, whether it comes from your onclick event or something else. sexradiogroup. Setoncheckedchangelistener (NewRadiogroup.oncheckedchangelistener () { Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) { Switch(checkedid) { CaseR.id.maleradiobuttonid:toast.maketext (Context,"You have chosen \" male \ ".", Toast.length_short). Show (); Break; CaseR.id.femaleradiobuttonid:toast.maketext (Context,"You chose \" female \ ".", Toast.length_short). Show (); Break; CaseR.id.sexradiobuttonid:toast.maketext (Context,"You chose \" Shemale \ ".", Toast.length_short). Show (); Break; default: Break; } } }); Submitbutton.setonclicklistener (NewView.onclicklistener () { Public voidOnClick (View v) {String sex=NULL; if(maleradiobutton.ischecked ()) {sex= "Male"; } if(femaleradiobutton.ischecked ()) {sex= "female"; } if(sexradiobutton.ischecked ()) {sex= "Shemale"; } toast.maketext (Context,"Your sex is:" +sex, Toast.length_short). Show (); } }); }}
2.xml file:
<?xml version= "1.0" encoding= "Utf-8"?><!--radio button page--><linearlayout xmlns:android= "/http" Schemas.android.com/apk/res/android "Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Vertical"android:padding= "5DP" > <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Gender:"android:textsize= "20sp"/> <Radiogroup Android:id= "@+id/sexradiogroupid"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" ><!--android:button= "@null" to customize Pictures-<RadioButton Android:id= "@+id/maleradiobuttonid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:checked= "true"Android:text= "Male"/> <RadioButton Android:id= "@+id/femaleradiobuttonid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Female"/> <RadioButton Android:id= "@+id/sexradiobuttonid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Shemale"/> </RadioGroup> </LinearLayout> <Button Android:id= "@+id/submitbuttonid"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Right"Android:text= "OK"android:textsize= "20SP"/></linearlayout>
3. Show:
radio button (RadioButton)