RadioButton CheckBox
The following example shows 2 features, one for the RadioButton selection, and one for the button button to view the properties of the 2 controls.
XML code
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_margin= "10DP" > <TablelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Match_parent" > <TableRowAndroid:id= "@+id/tablerow1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" > <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Right"Android:text= "Gender"android:textappearance= "? Android:attr/textappearancelarge" /> <RadiogroupAndroid:id= "@+id/radiogroup1" > <RadioButtonAndroid:id= "@+id/radiobutton1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Male" /> <RadioButtonAndroid:id= "@+id/radiobutton2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "female" /> </Radiogroup> </TableRow> <TableRowAndroid:id= "@+id/tablerow3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" > <TextViewAndroid:id= "@+id/textview2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Whether locals"android:textappearance= "? Android:attr/textappearancelarge" /> <CheckBoxAndroid:id= "@+id/checkbox1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /> </TableRow> <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "OK" /> </Tablelayout></Relativelayout>
Java code
Public classMainactivityextendsActivity {Button btn; Radiogroup RadioGroup1; CheckBox CheckBox1; protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.id.button1); RadioGroup1=(Radiogroup) Findviewbyid (r.id.radiogroup1); CheckBox1=(CheckBox) Findviewbyid (r.id.checkbox1); //button click eventBtn.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {intCheckedid=Radiogroup1.getcheckedradiobuttonid (); if(checkedid>0) {RadioButton R=(RadioButton) Findviewbyid (Checkedid); Toast.maketext (Getapplicationcontext (), R.gettext (), Toast.length_short). Show (); } toast.maketext (Getapplicationcontext (),""+checkbox1.ischecked (), Toast.length_short). Show (); } }); //selection events for RadiogruopRadiogroup1.setoncheckedchangelistener (NewOncheckedchangelistener () { Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) {RadioButton R=(RadioButton) Findviewbyid (Checkedid); Toast.maketext (Getapplicationcontext (),"You chose to be" +R.gettext (), Toast.length_short). Show (); } }); }}
Android Learning-Interface-ui-radiobutton CheckBox