Purpose of Study:
1, master How to build Radiogroup and RadioButton in Android
2, master the common properties of Radiogroup
3, understand the difference between RadioButton and checkbox
4. Mastering the event (listener) of Radiogroup selected state transformation
The difference between RadioButton and checkbox:
1, single RadioButton When selected, by clicking cannot become unchecked
When a single checkbox is selected, the click can become unchecked
2, a group of RadioButton, can only select one
A set of checkboxes that can simultaneously select multiple
3. RadioButton is represented by default in most UI frames
A checkbox is represented by default in most UI frames, in rectangles
The relationship between RadioButton and Radiogroup:
1. RadioButton represents a single round radio box, while Radiogroup is a container that can hold multiple radiobutton
2, each radiogroup in the RadioButton at the same time can have only one is selected
3, different radiogroup in the RadioButton is irrelevant, that is, if there is a selection in Group A, group B can still have a selected
4. In most cases, there are at least 2 radiobutton in a radiogroup.
5, most of the occasions, a radiogroup in the RadioButton will be selected by default, and it is recommended that you put it in the start position in Radiogroup
XML layout:
<?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:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Please select your gender:"android:textsize= "9pt" /> <RadiogroupAndroid:id= "@+id/radiogroup"android:contentdescription= "Gender"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"> <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/radiomale"Android:text= "Male"android:checked= "true"></RadioButton> <RadioButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:id= "@+id/radiofemale"Android:text= "female"></RadioButton> </Radiogroup><TextViewAndroid:id= "@+id/tvsex"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Your gender is: male"android:textsize= "9pt" /></LinearLayout>
Event listener for selected item changes:
When the selected items in the Radiogroup change, you may need to do some of the corresponding, such as the above example, gender selection "female" after the corresponding changes in the following article, or choose a different gender after the appearance of the gender profile list to update, girls do not like to use the beard as their avatar.
If you are unfamiliar with the listener, you can read the button in the Android control family and the Android listener.
The background code is as follows:
textview TV = null;//The text control to be changed according to the different options @Override public void OnCreate (Bundle Savedinstan Cestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Locate the text control based on the ID TV = (TextView) This.findviewbyid (r.id.tvsex); Locate the Radiogroup instance based on the id radiogroup group = (radiogroup) This.findviewbyid (R.id.radiogroup); Binds an anonymous listener group.setoncheckedchangelistener (new Oncheckedchangelistener () {@Override public void OnCheckedChanged (Radiogroup arg0, int arg1) {//TODO auto-generated method stub Gets the ID of the selected item after the change int radiobuttonid = Arg0.getcheckedradiobuttonid (); Obtain an instance of RadioButton by ID RadioButton RB = (RadioButton) MyActiviy.this.findViewById (Radiobuttonid); Update the text content to match the selected item Tv.settext ("Your gender is:" + rb.gettext ()); } }); }
The effect is as follows:
Summarize:
This article describes how to use Radiogroup and RadioButton in Android, compares the differences between RadioButton and checkboxes, and implements a custom Radiogroup change listener event that is selected radiobutton.
Android Control series Radiobutton&radiogroup "Go"