Android Basic Control Radiogroup

Source: Internet
Author: User

We often see a bunch of options on the phone, but we can only choose one, so in Android, the control is called RadioButton, which is the radio button meaning, they can only choose one effect, because there is a radiogroup such a component.

This time, we'll explain in detail the relationship and difference between the two controls, RadioButton and Radiogroup, and how to get them together in a tacit way.

To implement RadioButton consists of two parts , namely RadioButton and radiogroup , where Rdiogroup is a single-selection combo box , A container that can hold multiple radiobutton. in the absence of Radiogroup, RadioButton can be all selected, and when multiple RadioButton are radiogroup included, RadioButton can select only one, thus achieving the single-selection purpose. use setoncheckchangelinstener () to listen to the radio button.

Related properties of Radiogroup:

1.radiogroup.getcheckedradiobuttonid ();

This method can get the selected button

2.radiogroup.clearcheck ();

This method can clear the selected state

3.setOnCheckedChangeLintener (Radiogroup.oncheckedchangelistener listener);

callback method that is called when a radio button in a radio button group is selected when the state has changed

4.radiogroup.check (int id);

The method can be set by passing the ID to the selected state

5.addView (View child,int Index, viewgroup.layoutparams params);

Adds a word view using the specified layout parameters where child is the sub-view to add, index is the location where the child view will be added, and the layout parameter of the child view to be added by the params

6.radiobutton.gettext ();

Get the value of a radio box

  In addition, the checked property of RadioButton is set to True, RadioButton's check (ID) method is called in the code, and the OnCheckedChanged event is not triggered

Then, let's look at the relationship between RadioButton and radiogroup :

1.RadioButton represents a single prototype radio box, while Radiogroup is a container that can hold multiple radiobutton

2. RadioButton in each radiogroup can only have one selected

3. The radiobutton in different radiogroup are irrelevant, that is, if one of the group A is selected, then one of the group B can still be selected.

4. In most cases, there are at least two radiobutton in a Radiogroup

5. In most cases, a radiobutton in a Radiogroup is selected by default, and it is recommended that you place it in the starting position of the Radiogroup

Let's continue to look at the difference between the RadioButton and the checkbox (yes with the checkbox):

1. Once a single RadioButton is selected, the click 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 a circle by default in most UI frames

A checkbox is represented by default in most UI frames, in rectangles

Then, let's do a little case to further explain RadioButton and Radiogroup

Apart, let's start

Today, we use RadioButton and radiogroup to achieve this small case of ours.

  First of all, let's analyze what we want to do to get this case ready for writing.

According to the above diagram, we can see that we need pictures ~ But how many are needed? We need 6 of these things here! We look like three pictures, but when we click on it, we'll find that the picture is discolored, and that's another picture. So, we need to prepare 6 photos.

This is a resource.

Next, let's analyze what configuration files we all need to create

Don't think, we must first have a layout file of the main interface, and put three RadioButton, with a radiogroup to wrap up the three RadioButton, and set the corresponding properties

Then we need to create a configuration file that changes the font color in the drawable directory, and three profiles that change the picture.

Then, let's analyze what we should do in the activity.

First, we need to get to this radiogroup and then set the click event for it, in the Click event, we implement the Click button Pop up a toast

Well, our analysis is here for the time being, and then we're going to do the exact implementation based on our analysis.

First, let's start by creating the layout file for the main interface:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Application.smile.myapplicationdemo.MainActivity">        <RadiogroupAndroid:id= "@+id/rg"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:background= "@color/colorbackground"android:orientation= "Horizontal">        <RadioButtonAndroid:id= "@+id/radiobutton_xiaoxi"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:button= "@null"Android:drawabletop= "@drawable/bottom_icon_iamge_selector_xiaoxi"android:gravity= "Center"Android:text= "message"Android:textcolor= "@drawable/bottom_icon_text_selector" />        <RadioButtonAndroid:id= "@+id/radiobutton_lianxiren"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:button= "@null"Android:drawabletop= "@drawable/buttom_icon_image_selector_lianxiren"android:gravity= "Center"Android:text= "Contacts"Android:textcolor= "@drawable/bottom_icon_text_selector" />        <RadioButtonAndroid:id= "@+id/radiobutton_dongtai"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:button= "@null"Android:drawabletop= "@drawable/bottom_icon_image_selector_dongtai"android:gravity= "Center"Android:text= "dynamic"Android:textcolor= "@drawable/bottom_icon_text_selector" />    </Radiogroup></LinearLayout>

Here, let's explain the properties that are not described above

  The Android:drawabletop property is used to display the specified picture to the top of the RadioButton, so that we can put the text below

In the second step, let's look at the configuration file that changes the font color:

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <Itemandroid:state_checked= "false"Android:color= "@color/colorwhite"></Item> This is white .    <Itemandroid:state_checked= "true"Android:color= "@color/colorred"></Item> This is Orange .</selector>

In the third step, let's look at the configuration file that changes the three images:

Change "message":

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <Itemandroid:drawable= "@mipmap/xiaoxi_unchecked"android:state_checked= "false"></Item>    <Itemandroid:drawable= "@mipmap/xiaoxi_selected"android:state_checked= "true"></Item></selector>

Change "Contacts":

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <Itemandroid:state_checked= "false"android:drawable= "@mipmap/lianxiren_unchecked"></Item>    <Itemandroid:state_checked= "true"android:drawable= "@mipmap/lianxiren_selected"></Item></selector>

Change "dynamic":

<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android">    <Itemandroid:state_checked= "false"android:drawable= "@mipmap/dongtai_unchecked"></Item>    <Itemandroid:state_checked= "true"android:drawable= "@mipmap/dongtai_selected"></Item></selector>

Let's take a look at the properties in these files in detail

Where the Android:state_checked property is set state, true if it is selected, False otherwise. The corresponding android:drawable property is the corresponding picture

The fourth step, we look at the specific implementation of the activity:

 Public classMainactivityextendsAppcompatactivityImplementsradiogroup.oncheckedchangelistener{@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview (); }    Private voidInitview () {//through Findviewbyid () to find the radiogroup we needRadiogroup Radiogroup =(Radiogroup) Findviewbyid (R.ID.RG); //setting events for state changesRadiogroup.setoncheckedchangelistener ( This); }    /*** Override the method of changing the state of the event *@paramGroup Radio combo Box *@paramCheckedid The ID of each of these RadioButton*/@Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) {            //Pop different toast according to different ID        Switch(Group.getcheckedradiobuttonid ()) { CaseR.id.radiobutton_xiaoxi:toast.maketext ( This, "You clicked on the message button.", Toast.length_short). Show ();  Break;  CaseR.id.radiobutton_lianxiren:toast.maketext ( This, "You clicked the Contacts button.", Toast.length_short). Show ();  Break;  CaseR.id.radiobutton_dongtai:toast.maketext ( This, "You clicked on the" dynamic "button", Toast.length_short). Show ();  Break; }    }}

According to our thinking, we have implemented such a small case, let's test it.

In this way, we have achieved our small case perfectly.

Let the program write to life, the code into the soul

                    -------Smile, ZJ

Android Basic Control 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.