Xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.example.chenshuai.test322.UIActivity"> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please select what is the Android development language?" "android:padding= "10DP"/> <Radiogroup Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"Android:id= "@+id/rg"> <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "C + +"Android:id= "@+id/rb1"Android:layout_marginright= "30DP"android:checked= "true" /> <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "C"Android:id= "@+id/rb2"Android:layout_marginright= "30DP" /> <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "JAVA"Android:id= "@+id/rb3"Android:layout_marginright= "30DP" /> <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "C #"Android:id= "@+id/rb4" /> </radiogroup> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Please select font effect:"Android:id= "@+id/ziti"/> <CheckBoxAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Song Body"android:checked= "true"Android:id= "@+id/cb_song"/> <CheckBoxAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Bold"Android:id= "@+id/cb_cu"/> <CheckBoxAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Italic"Android:id= "@+id/cb_xie"/> <CheckBoxAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Underline"Android:id= "@+id/cb_xia"/></LinearLayout>
Java
Packagecom.example.chenshuai.test322;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.RadioButton;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.Toast; Public classUiactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_UI); Radiogroup Radiogroup=(Radiogroup) Findviewbyid (R.ID.RG); //Radiogroup Listener Event anonymous inner classRadiogroup.setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener () {@Override Public voidOnCheckedChanged (radiogroup Group, int Checkedid) {RadioButton RB=(RadioButton) Findviewbyid (Checkedid); if(Checkedid = =r.id.rb3) {toast.maketext (uiactivity. This, "the right choice.", Toast.length_long). Show (); } toast.maketext (uiactivity. This, Rb.gettext (), Toast.length_long). Show (); } }); CheckBox Cb_song=(CheckBox) Findviewbyid (R.id.cb_song); Cb_song.setoncheckedchangelistener (NewCboncheckedchangelistener ()); CheckBox CB_CU=(CheckBox) Findviewbyid (R.ID.CB_CU); Cb_cu.setoncheckedchangelistener (NewCboncheckedchangelistener ()); CheckBox Cb_xia=(CheckBox) Findviewbyid (R.id.cb_xia); Cb_xia.setoncheckedchangelistener (NewCboncheckedchangelistener ()); CheckBox Cb_xie=(CheckBox) Findviewbyid (R.id.cb_xie); Cb_xie.setoncheckedchangelistener (NewCboncheckedchangelistener ()); } //Listener event Inner class for CheckBox Private classCboncheckedchangelistenerImplements compoundbutton.oncheckedchangelistener {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {CheckBox CB=(CheckBox) Buttonview; if(isChecked) {toast.maketext (uiactivity. This, "checked" +Cb.gettext (), Toast.length_short). Show (); } Else{toast.maketext (uiactivity). This, "uncheck" +Cb.gettext (), Toast.length_short). Show (); } } }}
Android--radiogroup and checkboxes