Android Radiogroup and RadioButton detailed

Source: Internet
Author: User

The implementation of RadioButton consists of two parts, That is, RadioButton and Radiogroup are used together. Radiogroup is a single-selection combo box that can hold multiple RadioButton containers. In the absence of Radiogroup, RadioButton can all be selected; When multiple RadioButton are R Adiogroup contains the case, RadioButton can only select one. and use the Setoncheckedchangelistener to monitor the radio button.

Radiogroup Related properties:2 Radiogroup.getcheckedradiobuttonid (); --Get the ID of the selected button3 Radiogroup.clearcheck (); //---Clear the selected State 4 radiogroup.check (int id); //---Set this option by the parameter entry ID is selected if pass-1 is cleared as the specified selection identifier to clear the tick state of the radio button group, which is equivalent to calling the Clearcheck () operation //--A callback function to invoke when the status changes when the radio button in the radio button group is selected 6 AddView (View Child, int index, viewgroup.layoutparams params); //---Add a sub-view using the specified layout parameters 7 //parameter child to add a sub-view index to add a sub-view of the location of the  params to add the layout parameters of the child view 8 Radiobutton.gettext (); //Get the value of the Radio box 9 //In addition, the checked property of the RadioButton is set to True, and the code calls the check (ID) method of RadioButton. OnCheckedChanged events are not triggered

Case:

1. Define the layout file:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <scrollview xmlns:android= "http://schemas.android.com/apk/res/ Android "3 android:layout_width=" match_parent "4 android:layout_height=" Match_parent "> 5 <LinearLayout 6 android:orientation= "vertical" 7 android:layout_width= "match_parent" 8 Android:layo Ut_height= "Wrap_content" 9 android:layout_marginright= "5DP" >10 <TEXTVIEW11 android:id= "@+id/radiogroup_info_id" android:layout_width= "228px" android:layout_he ight= "Wrap_content" android:text= "I chose ...?" Android:textsize= "30sp"/> <RADIOGROUP18 android:id= "@+id/radiogroup_sex_id" android:layout_width= "Match_parent" 20 android:layout_height= "Match_parent" >22 <RadioButton23 android:id= "@+id/boy_id" android:layout_width= "Match_parent" 25               android:layout_height= "Match_parent" android:text= "Boy"/>28 <RadioButton29 android:id= "@+id/girl_id" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:text= "Girl"/>3 4 </radiogroup>35 <Button36 android:id= "@+id/radio_clear" Notoginseng android:layout_width= "match_p Arent "android:layout_height=" match_parent "android:text=" Clear the Check button ""/>41 <Button42 android:id= "@+id/ Radio_add_child "android:layout_width=" match_parent "android:layout_height=" match_parent "-android:text=" Add a single option "/>47 </linearlayout>48 </ScrollView>   

2.Java Code files

1 packageCom.dream.app.start.first.radiobutton; 2 ImportCOM.DREAM.APP.START.R; 3 ImportCom.dream.app.start.r.id; 4 ImportCom.dream.app.start.r.layout; 5 ImportCom.dream.app.start.three.utils.PublicClass; 6 Importandroid.app.Activity; 7 ImportAndroid.os.Bundle; 8 ImportAndroid.view.View; 9 ImportANDROID.VIEW.VIEW.ONCLICKLISTENER;10 ImportANDROID.VIEW.VIEWGROUP.LAYOUTPARAMS;11 ImportANDROID.WIDGET.BUTTON;12 ImportANDROID.WIDGET.RADIOBUTTON;13 ImportANDROID.WIDGET.RADIOGROUP;14 ImportANDROID.WIDGET.RADIOGROUP.ONCHECKEDCHANGELISTENER;15 ImportANDROID.WIDGET.TEXTVIEW;16 ImportANDROID.WIDGET.TOGGLEBUTTON;17 public class Radiobuttondemo extendsPublicclass {private TextView Textview=null; Private Radiogroup Radiogroup=null, privateRadioButton radiobutton_boy,radiobutton_girl;21 PrivateButton radio_clear,child;22/* (non-javadoc) * <a href= "http://my.oschina.net/u/244147" target= "_blank" rel= " nofollow "> @see </a>
* Android.app.activity#oncreate (Android.os.Bundle) 24 */25@Override26 protected voidOnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stub28 Super. OnCreate (savedinstancestate); 29Setcontentview (R.layout.layout_frist_radiobuton); TextView =(TextView) Findviewbyid (r.id.radiogroup_info_id); //RADIOGROUP32 radiogroup=(Radiogroup) Findviewbyid (r.id.radiogroup_sex_id); radiobutton_boy=(RadioButton) Findviewbyid (r.id.boy_id); radiobutton_girl=(RadioButton) Findviewbyid (r.id.girl_id); child=(Button) Findviewbyid (r.id.radio_add_child);//---37Radiogroup.setoncheckedchangelistener (listen); radio_clear=(Button) Findviewbyid (r.id.radio_clear); 39Radio_clear.setonclicklistener (OnClick); 40Child.setonclicklistener (OnClick); 41}42 Private Oncheckedchangelistener Listen=newOncheckedchangelistener () {43@Override44 public void OnCheckedChanged (radiogroup group, intCheckedid) {id= intGroup.getcheckedradiobuttonid (); switch(Group.getcheckedradiobuttonid ()) {caser.id.girl_id:48 Textview.settext ("I chose to be:" +Radiobutton_girl.gettext ());;r.id.boy_id:51 Textview.settext ("I chose to be:" + Radiobutton_boy.gettext ()); n-Break , and the default : Textview.settext ("I chose: New" ); }57 }58 };59 private Onclicklistener onclick=new Onclicklistener () {$ @Overri De61 public void OnClick (View v) {radio_clear= (Button) v;63 switch (Radio_clear.getid ()) {case< span> r.id.radio_clear:65 Radiogroup.check (-1);//clear option///Radiogroup.clearcheck (); Clear option Textview.settext ("I chose ...?") ); RadioButton (r.id.radio_add_child:70) //new sub-Newradio =new RadioButton ( Getapplicationcontext ()); Newradio.settext ("new" ); Radiogroup.addview (Newradio, Radiogroup.getchildcount ()); }79 break ;//76 default : All-in-a-break ;};81 }80

Operation Demonstration

the relationship between RadioButton and Radiogroup :

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

2, each radiogroup in the RadioButton at the same time can have only one is selected

3, different radiogroup in the RadioButton is irrelevant, that is, if there is a selection in Group A, group B can still have a selected

4. In most cases, there are at least 2 radiobutton in a radiogroup.

5, most of the occasions, a radiogroup in the RadioButton will be selected by default, and it is recommended that you put it in the start position in Radiogroup

The difference between RadioButton and checkbox:

1, single RadioButton When selected, by clicking 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 default in most UI frames

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

☆ Custom RadioButton Style

RadioButton What it looks like is determined by its background, button and other properties, the Android system
Use the style to define the default properties in the Android source code

The default definition can be seen in Android/frameworks/base/core/res/res/values/styles.xml:

1 <style name= "Widget.CompoundButton.RadioButton" > 2         <item name= "Android:background" > @android: Drawable/btn_radio_label_background</item> 3         <item name= "Android:button" > @android:d rawable/btn_ Radio</item> 4 </style>

That is, the background map is Btn_radio_label_background, and its button is Btn_radio

What is Btn_radio_label_background?
Its path is android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_label_background.9.png
Can see is a ninepatch picture, used to do the background, you can stretch the fill.

What is Btn_radio?

Its path is Android/frameworks/base/core/res/res/drawable/btn_radio.xml

is an XML-defined drawable that opens to look at its contents:

1 <selector xmlns:android= "Http://schemas.android.com/apk/res/android" > 2 <item android:state_checked= "T Rue "Android:state_window_focused=" false "3 android:drawable=" @drawable/btn_radio_on "/> 4 <item and Roid:state_checked= "false" Android:state_window_focused= "false" 5 android:drawable= "@drawable/btn_radio_off"/&  Gt 6 <item android:state_checked= "true" android:state_pressed= "true" 7 android:drawable= "@drawable/btn_radi O_on_pressed "/> 8 <item android:state_checked=" false "android:state_pressed=" true "9 android:drawab le= "@drawable/btn_radio_off_pressed"/> <item android:state_checked= "true" android:state_focused= "true"
android:drawable= "@drawable/btn_radio_on_selected"/> <item android:state_checked= "false" android:st Ate_focused= "true" android:drawable= "@drawable/btn_radio_off_selected"/> <item android:state_ch Ecked= "false" android:drawable= "@drawable/btn_radio_off"/> <item android:state_checked= "true" Android: drawable= "@drawable/btn_radio_on"/> </selector>

There are three ways of customizing:

1. Mode one:

1 <?xml version= "1.0" encoding= "Utf-8"?>     2 <selector xmlns:android= "Http://schemas.android.com/apk/res /android ">   3 <!--unchecked,   4     <item     5          android:state_checked=" false "     6          android:drawable= "@drawable/tabswitcher_long"/>  7 <!--selected,     8     <item     9         Android:state_checked= "true"    ten         android:drawable= "@drawable/tabswitcher_short"/> One    </ Selector> 

Using in the layout file

1 < ...  3 > 4 <  ... 6 android:button= "@null" 7 android:background= "@drawable/radio" 8/> 9 </RadioGroup> 

Android:button= "@null" to remove dots in front of RadioButton

2. Method Two: defined in Java code
@Override  2 Public boolean ontouchevent (Motionevent event) {3   if (event.getactionmasked () = =  Motionevent.action_down) {4     this. Setbackgroundresource (COM.WXG.TAB.R.DRAWABLE.MAIN_BG); 5   }else if (event.getactionmasked () = = Motionevent.action_down) {6 This      } 8 return Super. Ontouchevent (Event) ; 9}     

Remove radiobutton front of Dot adiobutton.setbuttondrawable (Android. R.color.transparent);

3. Mode Three

Using the XML file definition, use Radiobutton.setbackgroundresource (r.drawable.radio) in Java code;

==============================================================

Set RadioButton to the right of the text

1 <b><RadioButton  2             android:id= "@+id/button2"  3             android:layout_width= "Fill_parent"  4             android:layout_height= "50dip"  5             android:button= "@null"  6             android:drawableright= "@ Android:drawable/btn_radio "  //on the right 7             android:paddingleft=" 30dip "  8             android:text=" Android Master "  9             android:textsize= "20dip"/>   </b>

==============================================================

Android Radiogroup and RadioButton detailed

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.