Android-radiobutton Single Selection Demo

Source: Internet
Author: User

= = If not used, please change the JDK to 1.6

Code

 Packagecom.lxt008;ImportCOM.LXT008.R;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.Gravity;ImportAndroid.widget.RadioButton;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classActivity01extendsactivity{/*** Create a TextView object * Create a Radiogroup Object * Create 4 RadioButton Objects*/TextView M_textview;    Radiogroup M_radiogroup;    RadioButton m_radio1, M_radio2, M_radio3, M_radio4; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); /*** Get TextView Object * Get Radiogroup Object * Get 4 RadioButton Objects*/M_textview=(TextView) Findviewbyid (R.ID.TEXTVIEW01); M_radiogroup=(Radiogroup) Findviewbyid (R.ID.RADIOGROUP01); M_radio1=(RadioButton) Findviewbyid (R.id.radiobutton1); M_radio2=(RadioButton) Findviewbyid (R.id.radiobutton2); M_radio3=(RadioButton) Findviewbyid (R.id.radiobutton3); M_radio4=(RadioButton) Findviewbyid (R.ID.RADIOBUTTON4); /*setting up event snooping*/M_radiogroup.setoncheckedchangelistener (NewRadiogroup.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) {                //TODO auto-generated Method Stub                if(Checkedid = =M_radio2.getid ()) {Displaytoast ("Correct answer:" + m_radio2.gettext () + ", Congratulations, answer right! "); }                Else{displaytoast ("Please note that the answer is wrong!" ");    }            }        }); }        /*Show Toast*/     Public voiddisplaytoast (String str) {toast Toast= Toast.maketext ( This, str, toast.length_long); //set the location of the toast displayToast.setgravity (gravity.top, 0, 220); //Show the Toasttoast.show (); }}

Layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    >    <TextViewAndroid:id= "@+id/textview01"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/hello"    />  <RadiogroupAndroid:id= "@+id/radiogroup01"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "vertical"android:layout_x= "3px"android:layout_y= "54px"     >    <RadioButtonAndroid:id= "@+id/radiobutton1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/radiobutton1"    />    <RadioButtonAndroid:id= "@+id/radiobutton2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/radiobutton2"    />    <RadioButtonAndroid:id= "@+id/radiobutton3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/radiobutton3"    />    <RadioButtonAndroid:id= "@+id/radiobutton4"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/radiobutton4"    />    </Radiogroup>    </LinearLayout>

Android-radiobutton Single Selection Demo

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.