Radiogroup and checkboxes are common Android controls, and this article is a simple introduction and learning note, so the style of control used by the Android default style.
Let's look at the code implementation.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6C/F4/wKiom1VX_wLSz9CnAACRaA_UxPs218.jpg "title=" QQ picture 20150517103151.png "alt=" Wkiom1vx_wlsz9cnaacraa_uxps218.jpg "/>
In the figure, the top two (male and female) are two RadioButton in a Radiogroup, and the following three are checkboxes.
The contents of a radiogroup can only be selected, and the checkbox can be multiple.
Next is the Code section
Layout file Code activity_main.xml:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "Com.jam.radiogroupandcheckbox.MainActivity" > <radiogroup android:id= "@+id/id_radiogroup" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" > <radiobutton android:id= "@+id/ Id_radiobutton_male " android:layout_ Width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Male" /> <RadioButton android:id= "@+id/id_radiobutton_female" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "female" /> </RadioGroup> <checkbox android:id= "@+id/id_checkbox_one" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "one" /> <checkbox android:id= "@+id/id_checkbox_two" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:text= "both" /> <CheckBox android:id= "@+id/id_checkbox_three" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "three" /></LinearLayout>
Using a linear layout, the code is relatively simple, that is, a radiogroup contains two radiobutton (how many radiobutton you want to add), and three checkboxes.
Next is Mainactivity.java:
package com.jam.radiogroupandcheckbox;import android.app.activity;import android.os.bundle; import android.util.log;import android.view.menu;import android.widget.checkbox;import android.widget.compoundbutton;import android.widget.radiobutton;import android.widget.radiogroup; Import android.widget.radiogroup.oncheckedchangelistener;public class mainactivity extends activity {//Declaration Control private radiogroup radiogroup;private checkbox checkbox_one; private checkbox checkbox_two;private checkbox checkbox_three; @Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedinstancestate); SetContentView ( R.layout.activity_main);radiogroup = (radiogroup) findviewbyid (r.id.id_radiogroup); checkBox_one = (checkbox) findviewbyid (r.id.id_checkbox_one);checkbox_two = (checkbox) Findviewbyid (R.id.id_checkbox_two);checkbox_three = (CheckBox) findviewbyid (r.id.id_checkbox_three);/** * Radiogroup binds an anonymous inner class android.widget.radiogroup.oncheckedchangelistener */ Radiogroup.setoncheckedchangelistener (New oncheckedchangelistener () {@Overridepublic void OnCheckedChanged (Radiogroup group, int checkedid) {switch (checkedid) {case &NBSP;R.ID.ID_RADIOBUTTON_MALE:LOG.I ("Radiogroup", "male"); Break;case r.id.id_radiobutton_female: LOG.I ("Radiogroup", "female"); break;}}); /give three CheckBox binding listener Checkbox_one.setoncheckedchangelistener (New myoncheckedchangelistener ()); checkbox_ Two.setoncheckedchangelistener (New myoncheckedchangelistener ()); Checkbox_three.setoncheckedchangelistener (New myoncheckedchangelistener ());} /** * Note that the listener of the checkbox binding is android.widget.compoundbutton.oncheckedchangelistener * @ author jam * */private class myoncheckedchangelistener implements android.widget.compoundbutton.oncheckedchangelistener {@Overridepublic void oncheckedchanged ( compoundbutton buttonview,boolean ischecked) {switch (Buttonview.getid ()) {case R.ID.ID_CHECKBOX_ONE:LOG.I ("checkbox", "one"), BREAK;CASE&NBSP;R.ID.ID_CHECKBOX_TWO:LOG.I ("checkbox", "BREAK;CASE&NBSP;R.ID.ID_CHECKBOX_THREE:LOG.I" ("checkbox", "three"); break;}}}
The above is the simple use of radiogroup and checkbox, Radiogroup and checkbox to get the choice of content and other uses of the online already have a lot of resources, no longer described here.
This article is from the "good Ask Yu" blog, please be sure to keep this source http://imjam.blog.51cto.com/9871131/1652041
Android Learning notes: Common Controls Radiogroup and checkboxes