Android Tenth Lesson--ui RadioButton

Source: Internet
Author: User

Common Base Empty components

5 Radiogroup RadioButton

Radio button is one of the most common components, but although many areas of the development process will use the radio button, but will not use the original Android system style, similar to when we write HTML, different browsers provide the radio button is not the same, the compatibility is poor, the general picture instead of camouflage. However, regardless of the camouflage style, the radio button functions are the same. The following is a subtraction operator that explains the function of the radio button, as the style is provided later.

Components in Layout:

 <radiogroup        android:id= "@+id/operation"          android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"         android:o rientation= "Horizontal"         >       <radiobutton          android:id= "@+id/addition"           android:layout_width= "Wrap_content"            android:layout_height= "Wrap_content"            android:checked= "true"            android:text= "@string/addition"/>      <radiobutton         &Nbsp; android:id= "@+id/subtraction"           android: Layout_width= "Wrap_content"           android:layout_height= " Wrap_content "          android:text=" @string/subtraction "/ >    </RadioGroup>

Components in activity:

   protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);        //  Get radiogroup        radiogroup =  (RadioGroup) This.findviewbyid (r.id.operation);        //  Register Event Listener for Radiogroup (the OnCheckedChanged method of this class is triggered when the radio button is selected)          Radiogroup.setoncheckedchangelistener (New mainactivity.radiogroupdefineimpl ());     }         // activity Inner class, Implement Oncheckedchangelistener interface and implement OnCheckedChanged method     class RadioGroupDefineImpl  implements oncheckedchangelistener{@Overridepublic  void oncheckedchanged (Radiogroup group, &Nbsp;int id)  {// TODO Auto-generated method stub//  Group is the Radiogroup object that the radio belongs to, the ID is the ID of the radio that is selected, so you can get the RadioButton by the following way, You can also get idradiobutton radio =  (RadioButton) via Radiogroup.getcheckedradiobuttonid () MainActivity.this.findViewById (ID); Toast.maketext (Mainactivity.this, radio.gettext (),  toast.length_short). Show ();         }

Attention:

1) radio button RadioButton can not exist alone, he must have a clear grouping radiogroup, that is, each RadioButton should be radiogroup sub-components.

2) register an ID for each radiogroup and RadioButton for easy access in Java programs

3) checked= "true" means that the button is selected by default, it is best to have a default option when using a radio button, otherwise why don't you use the multi-select button?

4) In the Java program activity of the OnCreate method we only need to get radiogroup, and then for Radiogroup Register event Listener, this event listener is a Oncheckedchangelistener instance object, At the same time we want to implement the OnCheckedChanged method. In the listener we can get which RadioButton is selected by the parameter. This is the biggest difference from the button or checkbox, and they are all on separate buttons for registering event listeners. You can also get the ID of the selected radiobutton by Radiogroup.getcheckedradiobuttonid ().

5) When we specify in the Java program that a radio button is selected, the general use ID is judged as follows:

int operation = Radiogroup.getcheckedradiobuttonid () switch (operation) {case r.id.subtraction://coding break; }

Here is a subtraction operator that uses RadioButton:

Layout.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"       >    <EditText         android:id= "@+id/param1"         android:layout_width = "Match_parent"         android:layout_height= "Wrap_content"          android:text= "@string/defalut0"          android:inputtype= "Number"         />         <radiogroup         android:id= " @+id/operation "  &Nbsp;     android:layout_width= "Match_parent"          android:layout_height= "Wrap_content"         android:o rientation= "Horizontal"         >       <radiobutton          android:id= "@+id/addition"           android:layout_width= "Wrap_content"            android:layout_height= "Wrap_content"            android:checked= "true"            android:text= "@string/addition"/>      <radiobutton           android:id= "@+id/subtraction"            android:Layout_width= "Wrap_content"           android:layout_height= " Wrap_content "          android:text=" @string/subtraction "/ >      <RadioButton           android:id= "@+id/multiplication"           android: Layout_width= "Wrap_content"           android:layout_height= " Wrap_content "          android:text=" @string/multiplication " />      <radiobutton           android:id= "@+id/division"           android:layout_ Width= "Wrap_content"           android:layout_height= "Wrap_ Content "&NBSP;&NBSP;&Nbsp;       android:text= "@string/division"/>       </RadioGroup>        <EditText           android:id= "@+id/param2"            android:layout_width= "Match_parent"            android:layout_height= "Wrap_content"           android: Inputtype= "number"           android:text= "@string/defalut0"/ >        <Button         Android:id= "@+id/mainactivity_btn"         android:layout_width= "Match_ Parent "         android:layout_height=" Wrap_content "         android:text= "@string/mainactivity_result"         />     <textview android:id= "@+id/result"          android:layout_width= "Match_parent"         android:layout_height = "Match_parent"         android:text= "@string/defalut0"/>     </LinearLayout>

Java Activity:

Package com.hnust.moon;import android.os.bundle;import android.support.v7.app.actionbaractivity ;import android.view.menu;import android.view.menuitem;import android.view.view;import  Android.view.view.onclicklistener;import android.widget.button;import android.widget.edittext;import  android.widget.RadioButton;import android.widget.RadioGroup;import  android.widget.radiogroup.oncheckedchangelistener;import android.widget.textview;import  Android.widget.toast;public class mainactivity extends actionbaractivity {private  static final string tag = mainactivity.class.getsimplename ();p rivate  radiogroup radiogroup;private button btn;private edittext param1;private  edittext param2;private textview rsv;     @Override      Protected void oncreate (bundle savedinstancestate) &nbsP {        super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);         param1 =  (EditText) MainActivity.this.findViewById (r.id.param1);         param2 =  (EditText) MainActivity.this.findViewById (r.id.param2);         rsv =  (TextView) MainActivity.this.findViewById (r.id.result);                 btn =  ( Button) This.findviewbyid (R.ID.MAINACTIVITY_BTN);         Btn.setonclicklistener (New mainactivity.buttondefineimpl ());                 radioGroup =  (Radiogroup) This.findviewbyid ( r.id.operation);     &nbsP;   radiogroup.setoncheckedchangelistener (New mainactivity.radiogroupdefineimpl ());     }        class RadioGroupDefineImpl  implements oncheckedchangelistener{@Overridepublic  void oncheckedchanged (Radiogroup group,  int id)  {// todo auto-generated method stubradiobutton radio =   (RadioButton) MainActivity.this.findViewById (ID); Toast.maketext (Mainactivity.this, radio.gettext (),  toast.length_short). Show ();         }        /**      *      *  @author  heweipo      *     */    class ButtonDefineImpl  implements onclicklistener{@Overridepublic  void onclick (view view)  {// todo  auto-generated method stub        int operation =  radiogroup.getcheckedradiobuttonid (); Rsv.settext (Calcurateresult (Operation, integer.parseint ( Param1.gettext (). ToString ()),  integer.parseint (Param2.gettext (). ToString ())) + "");     }        private int calcurateresult (INT&NBSP;OPERATION&NBSP;,&NBSP;INT&NBSP;PARAM1&NBSP;,&NBSP;INT&NBSP;PARAM2) {    int result = 0;    switch (operation) {     case R.id.subtraction:    result = param1 - param2;     break;case R.id.multiplication:result = param1 * param2;     break;case r.id.division:if (param2 == 0) {Toast.makeText (this,  " Divisor cannot be 0 ",  toast.length_short). Show (); break;} result = param1 / p aram2;break;default:result = param1 + param2;break;    }     return result;    }              @Override     public boolean oncreateoptionsmenu (menu menu )  {            menu.add (0, 8080, 1,  r.string.exit);         getmenuinflater (). Inflate (R.menu.main,  menu);        return true;    }      @Override     public boolean onoptionsitemselected (MenuItem  item)  {        int id = item.getitemid ();         switch  (ID)  {case R.id.action_settings:break; Case r.id.menu_addiTion:radioGroup.check (r.id.addition); Rsv.settext (Calcurateresult (R.id.addition, integer.parseint ( Param1.gettext (). ToString ()),  integer.parseint (Param2.gettext (). ToString ())) + "");break;case  R.id.menu_subtraction:radiogroup.check (r.id.subtraction); Rsv.settext (Calcurateresult (R.id.subtraction,  Integer.parseint (Param1.gettext (). ToString ()),  integer.parseint (Param2.gettext (). ToString ())) + ""; Case r.id.menu_multiplication:radiogroup.check (r.id.multiplication); Rsv.settext (CalcurateResult ( R.id.multiplication, integer.parseint (Param1.gettext (). toString ()),  integer.parseint (Param2.getText ( ) (ToString ())) + ""); Break;case r.id.menu_division:radiogroup.check (r.id.division); Rsv.settext ( Calcurateresult (R.id.division, integer.parseint (Param1.gettext (). toString ()),  integer.parseint ( Param2.gettext (). toString ()) + ""); Break;case 8080:finish (); break;}         return super.onoptionsitemselected (item);    }   } 

Image:

Android Tenth Lesson--ui RadioButton

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.