Android control series radiobutton and radiogroup use method detailed reference: http://www.jb51.net/article/31799.htm

Source: Internet
Author: User

Objective:

1. Master how to establish radiogroup and radiobutton in Android
2. Master common radiogroup attributes
3. Understand the differences between radiobutton and checkbox
4. Master the events (listener) of radiogroup selected status change)

 

What is the difference between radiobutton and checkbox:

1. After a single radiobutton is selected, it cannot be changed to unselected by clicking
After a single checkbox is selected, it can be changed to unselected by clicking
2. A group of radiobutton can only be selected at the same time
A group of checkpoints that can be selected at the same time
3. radiobutton is represented in a circle by default in most UI frameworks.
In most UI frameworks, checkbox is represented by a rectangle by default.
The relationship between radiobutton and radiogroup:
1. radiobutton indicates a single circle, while radiogroup is a container that can hold multiple radiobutton.
2. Only one radiobutton in each radiogroup can be selected.
3. radiobutton in different radiogroups is irrelevant. That is, if one of group A is selected, one of group B can still be selected.
4. In most cases, a radiogroup contains at least two radiobutton
5. In most cases, radiobutton in a radiogroup will be selected by default. We recommend that you place it in the starting position of the radiogroup.

XML layout:

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <linearlayout xmlns: 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 <textview 8 Android: layout_width = "fill_parent" 9 Android: layout_height = "wrap_content" 10 Android: text = "select your gender: "11 Android: textsize =" 9pt "12/> 13 <radiogroup Android: Id =" @ + ID/radiogroup "Android: contentdescription =" gender "Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> 14 <radiobutton Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: id = "@ + ID/radiomale" Android: text = "male" Android: checked = "true"> </radiobutton> 15 <radiobutton Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/radiofemale" Android: text = "female"> </radiobutton> 16 </radiogroup> 17 <textview 18 Android: id = "@ + ID/tvsex" 19 Android: layout_width = "fill_parent" 20 Android: layout_height = "wrap_content" 21 Android: text = "your gender is: male "22 Android: textsize =" 9pt "23/> 24 </linearlayout>

Event listening for the selected change:

After the selected item in the radiogroup is changed, you may need to make some corresponding changes. For example, in the above example, after selecting "female" as the gender, the following article also changes accordingly, or, after you select a different gender, a list of headers that match the gender is displayed and updated. Girls do not like to use a bearded beard as their Avatar.

If you are not familiar with the listener, you can read the buttons of the android control series and the android listener.

The background code is as follows:

1 textview TV = NULL; // The text control to be changed based on different options 2 @ override 3 Public void oncreate (bundle savedinstancestate) {4 super. oncreate (savedinstancestate); 5 setcontentview (R. layout. main); 6 // locate the text control 7 TV = (textview) This by ID. findviewbyid (R. id. tvsex); 8 // locate the radiogroup instance 9 radiogroup group = (radiogroup) This. findviewbyid (R. id. radiogroup); 10 // bind an anonymous listener 11 Group. setoncheckedchangelistener (New oncheckedchangelistener () {12 13 @ override 14 public void oncheckedchanged (radiogroup arg0, int arg1) {15 // todo auto-generated method stub 16 // obtain the ID 17 int radiobuttonid = arg0.getcheckedradiobuttonid () of the selected item after the change (); 18 // obtain the radiobutton instance 19 radiobutton RB = (radiobutton) myactiviy according to the ID. this. findviewbyid (radiobuttonid); 20 // update the text content to conform to the selected item 21 TV. settext ("your gender is:" + RB. gettext (); 22} 23}); 24}

The effect is as follows:

 

Detailed source reference: http://www.jb51.net/article/31799.htm

Summary:

This article describes how to use radiogroup and radiobutton in Android, compares the differences between radiobutton and checkbox, and implements the change listening event of the radiobutton selected in the Custom radiogroup.

Detailed source reference: http://www.jb51.net/article/31799.htm

Detailed source reference: http://www.jb51.net/article/31799.htm

 

Detailed source reference: http://www.jb51.net/article/31799.htm

Related Article

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.