Similar to the button, Cb1.setoncheckedchangelistener (this) is used here, and the method Checkchange event bindings for 3 checkboxes respectively. It then fetches the state of the Click CheckBox in the OnCheckedChanged abstract function and displays it with a toast.
1 //checking listeners with state changes2 Public classMainactivityextendsActivityImplementsOncheckedchangelistener {3 PrivateCheckBox CB1, CB2, CB3;//Create 3 checkbox objects4 5 @Override6 Public voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.main);9 //Instantiate a 3 checkboxTenCB1 =(CheckBox) Findviewbyid (R.ID.CB1); OneCB2 =(CheckBox) Findviewbyid (R.ID.CB2); ACB3 =(CheckBox) Findviewbyid (R.ID.CB3); -Cb1.setoncheckedchangelistener ( This); -Cb2.setoncheckedchangelistener ( This); theCb3.setoncheckedchangelistener ( This); - } - - //overriding the listener's abstract function + @Override - Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { + //Buttonview Select the button that has changed A //isChecked indicates the new state of the button (True/false) at if(CB1 = = Buttonview | | cb2 = Buttonview | | cb3 =Buttonview) { - if(isChecked) { - //display a hint message -Toastdisplay (Buttonview.gettext () + "checked"); - -}Else { inToastdisplay (Buttonview.gettext () + "uncheck"); - } to } + } - the Public voidToastdisplay (String str) { *Toast.maketext ( This, str, toast.length_short). Show (); $ }Panax Notoginseng}
With that in mind, let's take a look at the usage of RadioButton: Note that the different places here are the 13th, 14 rows, not like a checkbox. Each control binds the Checkchange listener, but binds the radiogroup.
1 //Use status change listener2 Public classMainactivityextendsActivityImplementsOncheckedchangelistener {3 PrivateRadioButton Rb1, Rb2, Rb3;4 PrivateRadiogroup RG;5 6 @Override7 Public voidonCreate (Bundle savedinstancestate) {8 Super. OnCreate (savedinstancestate);9 Setcontentview (r.layout.main);TenRB1 =(RadioButton) Findviewbyid (R.ID.RB1); OneRB2 =(RadioButton) Findviewbyid (R.ID.RB2); ARB3 =(RadioButton) Findviewbyid (R.ID.RB3); -RG =(Radiogroup) Findviewbyid (R.ID.RADGRP); -Rg.setoncheckedchangelistener ( This);//to bind a radio group to a listener the } - - //overriding listener functions - /** + * @paramGroup : Refers to radio groups - * @paramGroup: Refers to the memory of a RadioButton in a radio group that has a state change id! + */ A @Override at Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) { - if(Group = = RG) {//because there is only one radiogroup in the current program, this step can not be determined -String Rbname =NULL; - if(Checkedid = =Rb1.getid ()) { -Rbname =Rb1.gettext (). toString (); -}Else if(Checkedid = =Rb2.getid ()) { inRbname =Rb2.gettext (). toString (); -}Else if(Checkedid = =Rb3.getid ()) { toRbname =Rb3.gettext (). toString (); + } -Toast.maketext ( This, "Select the radio button labeled" "+ Rbname +", the Toast.length_long). Show (); * } $ }Panax Notoginseng}
So how does this radiogroup combine with RadioButton to show the effect of multiple choices? Let's look at the XML and we'll know:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 android:orientation= "vertical"4 Android:layout_width= "Fill_parent"5 Android:layout_height= "Fill_parent"6 >7 <Radiogroup8 Android:id= "@+id/radgrp"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "Wrap_content"> One <RadioButton A Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" - Android:text= "RadioButton1" the Android:id= "@+id/rb1" - /> - <RadioButton - Android:layout_width= "Fill_parent" + Android:layout_height= "Wrap_content" - Android:text= "RadioButton2" + Android:id= "@+id/rb2" A /> at <RadioButton - Android:layout_width= "Fill_parent" - Android:layout_height= "Wrap_content" - Android:text= "RadioButton3" - Android:id= "@+id/rb3" - /> in </Radiogroup> - </LinearLayout>
This article link: http://www.cnblogs.com/zjutlitao/p/4229767.html
More Highlights: http://www.cnblogs.com/zjutlitao/
[Android] 4, CheckBox, RadioButton and toast simple usage