UI control RadioButton (radio button) &checkbox (check button)

Source: Internet
Author: User

(i) Overview:

(ii) Basic usage and event handling of RadioButton:



Implementation code:
XML file

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:orientation="vertical" >                <TextViewandroid:text="Gender:"android:layout_width="Match_parent"  android:layout_height="wrap_content"/>                             <radiogroup android:id= "@+id/sex"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content ">                        <RadioButtonandroid:text="male"android:layout_width="Wrap_content"  android:layout_height="wrap_content"/>                                              <RadioButtonandroid:text= "female"android:layout_width="Wrap_ Content "android:layout_height=" wrap_content "/>                                         </radiogroup>    <buttonandroid:id="@+id/sexid"android:layout_width="Match_ Parent "android:layout_height=" Wrap_content "android:text=" Select Gender "/>                                  </linearlayout>

Mainactivity.java

 Public  class radiobuttontest extends Activity {    PrivateRadiogroup Rgroup;PrivateButton Rbutton;@Override    protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub        Super. OnCreate (Savedinstancestate);        Setcontentview (r.layout.radio_button_test);        Rgroup = (radiogroup) Findviewbyid (r.id.sex);        Rbutton = (Button) Findviewbyid (R.ID.SEXID); Rbutton.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {//TODO auto-generated method stub                intLen = Rgroup.getchildcount ();//Get the number of options for a radio button groupString msgstring =""; for(inti =0; I <len; i++) {RadioButton RadioButton = (RadioButton) rgroup.getchildat (i);if(Radiobutton.ischecked ()) {msgstring = Radiobutton.gettext (). toString (); Break; }} toast.maketext (Radiobuttontest. This, Msgstring, Toast.length_short). Show ();    }        }); }}

Code parsing:
Here we bind the Setonclicklistener event listener for the "Select Gender" button, iterate through each click, radiogroup Determine which button is selected we can get RadioButton information by the following methods!
Getchildcount () Gets the number of radio buttons in the button group;
Getchindat (i): Get our radio button according to the index
IsChecked (): Determines whether the button is selected;

Of course, I'm here to bind the clicked event of the selected value to the other button that is not RadioButton, but you can also bind the listener to the RadioButton itself.

Just like this:

You have chosen "+radbtn.gettext (), Toast.length_long)." Show ();
}
});

(c) The basic usage and event handling of the checkbox (check button):

Operation Result:

The first method: Bind Onclicklistener () to implement
Implementation code:

The XML file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent"  Android:orientation="vertical" >                <TextViewandroid:text="Gender:"android:layout_width=" Match_parent "android:layout_height=" wrap_content "/>                             <radiogroup android:id= "@+id/sex"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content ">                        <RadioButtonandroid:text= "male"android:layout_width="Wrap_ Content "android:layout_height=" wrap_content "/>                                              <RadioButtonandroid:text="female"android:layout_width="Wrap_content"  android:layout_height="wrap_content"/>                                         </radiogroup>    <buttonandroid:id="@+id/sexid"android:layout_width="Match_ Parent "android:layout_height=" Wrap_content "android:text=" Select Gender "/>                                  </linearlayout>

The Java files are as follows:

/** * checkbox is unchecked by default, if you want to modify this default, * You can set the android:checked in <checkbox> to True * Or, you can use the Checkbox.setchecked method to set the function to enable the check. * @author eillot * * * * Public  class checkboxtest extends Activity implements Onclicklistener {        PrivateList<checkbox> Checkboxs =NewArraylist<checkbox> ();@Override        protected void onCreate(Bundle savedinstancestate) {//TODO auto-generated method stub            Super. OnCreate (Savedinstancestate);//setcontentview (r.layout.check_box_test);String cstring[] =Newstring[]{"Who are you?","I'm the man who's going to be an architect.","When are you going to do that?","I'll be there in a minute."};//dynamic load layoutLinearLayout linearlayout = (linearlayout) getlayoutinflater (). Inflate (R.layout.check_box_test,NULL);//Assign a value to the specified checkbox             for(inti =0; i < cstring.length; i++) {//Get the Checkbox.xml object firstCheckBox checkbox = (checkbox) Getlayoutinflater (). Inflate (R.layout.checkbox,NULL);                Checkboxs.add (CheckBox); Checkboxs.get (i). SetText (Cstring[i]);//implemented inLinearlayout.addview (Checkbox,i);            } setcontentview (LinearLayout);            Button button = (button) Findviewbyid (R.id.ensurenbutton); Button.setonclicklistener ( This); }@Override         Public void OnClick(View v) {//TODO auto-generated method stubString s =""; for(CheckBox Checkbox:checkboxs) {if(Checkbox.ischecked ()) {s + = Checkbox.gettext () +"\ n"; }            }if(" ". Equals (s)) {s ="You have not checked the item!!"; }//Use a hint box to prompt the user for information            NewAlertdialog.builder ( This). Setmessage (s). Setpositivebutton ("Off",NULL). Show (); }}

The second method: Bind Setoncheckedchangedlistener () to implement
XML file is not posted, too simple, Java code is as follows:



(iv) Custom click Effects

To run a picture:

Implementation code:



(v) Change the relative position of the font with the selection box

(vi) Change the distance between text and selection box

Just like this:

< finish >

UI control RadioButton (radio button) &checkbox (check button)

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.