radio button (RadioButton)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.