How to Use RadioButton and RadioGroup In the Android control series

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:

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "select your gender :"
Android: textSize = "9pt"
/>
<RadioGroup android: id = "@ + id/radioGroup" android: contentDescription = "gender" android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<RadioButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/radioMale" android: text = "male" android: checked = "true"> </RadioButton>
<RadioButton android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/radioFemale" android: text = ""> </RadioButton>
</RadioGroup>
<TextView
Android: id = "@ + id/tvSex"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "your gender is: Male"
Android: textSize = "9pt"
/>
</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:Copy codeThe Code is as follows: TextView TV = null; // The text control to be changed based on different options
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Locate the text control by ID
TV = (TextView) this. findViewById (R. id. tvSex );
// Locate the RadioGroup instance by ID
RadioGroup group = (RadioGroup) this. findViewById (R. id. radioGroup );
// Bind an anonymous listener
Group. setOnCheckedChangeListener (new OnCheckedChangeListener (){

@ Override
Public void onCheckedChanged (RadioGroup arg0, int arg1 ){
// TODO Auto-generated method stub
// Obtain the ID of the selected item after the change
Int radioButtonId = arg0.getCheckedRadioButtonId ();
// Obtain the RadioButton instance by ID
RadioButton rb = (RadioButton) MyActiviy. this. findViewById (radioButtonId );
// Update the text to match the selected item
TV. setText ("your gender is:" + rb. getText ());
}
});
}

The effect is as follows:

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.

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.