Android common Controls Radiogroup,radiobutton,checkbox and toast

Source: Internet
Author: User
Tags stub

I. Radiogroup and RadioButton

The RadioButton radio button is a dual-state button that can be selected or unchecked. When the radio button is not selected, the user can click or click to select it. However, in contrast to the checkbox, the user is not able to uncheck through the interface once it is selected, but it can be unchecked by code.

Radiogroup is used to create a group of radio groups of selected states that are mutually exclusive. In a group, only one button can be selected, and a RadioButton is selected to automatically cancel the selection of other buttons. In the initial state, all radio buttons are not checked, although it is not possible to cancel the check state of a specific radio button, but it can be removed by the radio button group. Radiogroup and RadioButton are usually used together.

Interface Radiogroup.oncheckedchangelistener is the interface class of the callback function that is invoked when the tick state of the radio button in the radio button group changes. Add a listener for Radiogroup when we want to do the appropriate action when the RadioButton button in the Radiogroup changes the check state. In this class we need to duplicate the public void oncheckedchanged (radiogroup group, int checkedid) method, which has two parameters for the passed in group and the ID of the RadioButton selected in the group. We can use this ID to determine which button is selected to perform the appropriate action.

Second, CheckBox

A checkbox inherits from Compoundbutton and is a check box with a dual-state button that can be selected or unchecked. A checkbox has no concept of a group and can be used independently. You can add listeners to the checkbox, and you need to copy the public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) method in the listener's code. The two parameters of the method are Compoundbutton and checked, because the checkbox inherits from Compoundbutton and is a Compoundbutton subclass, so it can be converted to Compoundbutton.

Third, Toast

Toast is a view that provides concise information to the user. The Toast class helps you create and display this information. The view is presented to the user in a form that floats above the application. Because it does not get the focus, even if the user is typing nothing will be affected. Its goal is to be as unobtrusive as possible so that users can see the information you provide. The simplest way to use this control is to invoke the static method of the class public static Toast Maketext (the context, charsequence text, int duration) Returns a Toast object, Then you can call the show () method. such as Toast. Maketext (Context context, charsequence text, int duration). Show (). The first parameter of this method is the context object, usually your application or activity object, the second argument is the text to display, the text can be formatted, and the third parameter is how long it lasts to display the message with two constants: Length_short or Length_ LONG.

Example:

Create a new Android application. Write the Main.xml file and add a radiogroup label (you can also set whether the layout orientation within this label is horizontal or vertical). Add two RadioButton to the Radiogroup tab. Then add the 3 checkbox check box. When we click on the radio button or select the check box will prompt the appropriate information.

Android_select.java

01.package com.idea.org;
03.import android.app.Activity;
04.import Android.os.Bundle;
05.import Android.widget.CheckBox;
06.import Android.widget.CompoundButton;
07.import Android.widget.RadioButton;
08.import Android.widget.RadioGroup;
09.import Android.widget.Toast;    11.public class Android_select extends activity {12.
Private Radiogroup Radiogroup=null;
Private RadioButton Radiobuttonfemale=null;
Private RadioButton Radiobuttonmale=null;
Private CheckBox Checkboxswim=null;
Private CheckBox Checkboxrun=null;
Private CheckBox Checkboxread=null; /** called the activity is a.    * * 19.    @Override 20.        public void OnCreate (Bundle savedinstancestate) {21.
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
radiogroup= (Radiogroup) Findviewbyid (R.id.radiogroup);
Radiobuttonfemale= (RadioButton) Findviewbyid (R.id.radiobuttonfemale); Radiobuttonmale= (radiobutton) Findviewbyid (R.id.radiobuttonmale);
Checkboxswim= (CheckBox) Findviewbyid (r.id.checkboxswim);
Checkboxrun= (CheckBox) Findviewbyid (R.id.checkboxrun);
Checkboxread= (CheckBox) Findviewbyid (R.id.checkboxread);        29.//Add Listener 30 for Radiogroup.
Radiogroup.setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener () {31.            @Override 33.                public void OnCheckedChanged (radiogroup group, int checkedid) {34.                TODO auto-generated Method Stub 35.                if (Radiobuttonfemale.getid () ==checkedid) 36.                    {37.//Call Toast static method to display prompt message 38.
Toast.maketext (Android_select.this, "female", Toast.length_short). Show ();                39.} 40.                else if (Radiobuttonmale.getid () ==checkedid) 41.
{Toast.maketext (Android_select.this, "male", Toast.length_short). Show ();            43.} 44.        } 45.
}); 46.//Add listener 47 to the check box.
Checkboxswim.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {48.            @Override 50.                public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {51.                TODO auto-generated Method Stub 52.                    if (Ischecked==true) 53.
Toast.maketext (Android_select.this, "swim", Toast.length_short). Show ();        54.} 55.
});
Checkboxrun.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {57.            @Override 59.                public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {60.                TODO auto-generated Method Stub 61.                    if (ischecked==true) 62.
Toast.maketext (Android_select.this, "Running", Toast.length_short). Show ();        63.} 64.
});
Checkboxread.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {66. @OverriDe 68.            public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {69.            TODO auto-generated Method Stub 70.                if (ischecked==true) 71.
Toast.maketext (Android_select.this, "reading", Toast.length_short). Show ();    72.} 73.
}); 74.} 75.}

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.