Android Control: RadioButton (radio button)

Source: Internet
Author: User



First, a radiogroup is registered in the layout file Activity_main.xml, and the Radiogroup is set for listening, and two RadioButton in the figure is a radiogroup.


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:orien tation= "Vertical" tools:context= ". Mainactivity "> <textview android:text=" @string/hello_world "android:layout_width=" Wrap_content "and roid:layout_height= "Wrap_content"/> <radiogroup android:id= "@+id/radiogroupid" Android:layout_widt H= "Wrap_content" android:layout_height= "wrap_content" > <radiobutton android:id= "@+id/femal Egroupid "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Androi d:text= "female"/> <radiobutton android:id= "@+id/malegroupid" android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:text= "male"/> </radiogro up></linearlayout> 



Mainactivity.java

Import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.widget.radiobutton;import Android.widget.radiogroup;public class MainActivity    Extends Actionbaractivity {private Radiogroup radiogroup;    Private RadioButton Femaleradiobutton;    Private RadioButton Maleradiobutton;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Load Layout file Setcontentview (r.layout.activity_main);        Get instance radiogroup= (Radiogroup) Findviewbyid (r.id.radiogroupid);        femaleradiobutton= (RadioButton) Findviewbyid (r.id.femalegroupid);        maleradiobutton= (RadioButton) Findviewbyid (r.id.malegroupid);    Set the monitoring Radiogroup.setoncheckedchangelistener (new Radiogrouplistener ()); }//define a Radiogroup Oncheckedchangelistener//radiogroup bound by the Radiogroup.oncheckedchangelistener//CheckBox bound It's Compoundbutton.oncheckedchangeli.Stener or view.  Onclicklistener class Radiogrouplistener implements radiogroup.oncheckedchangelistener{@Override public                void OnCheckedChanged (radiogroup group, int checkedid) {if (Checkedid==femaleradiobutton.getid ()) {            System.out.println ("female! selected");            }else if (Checkedid==maleradiobutton.getid ()) {System.out.println ("male! selected"); }}} @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds item        s to the action Bar if it is present.        Getmenuinflater (). Inflate (R.menu.menu_main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid (); Noinspection simplifiableifsTatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}


The following are examples of log presentation samples:






Android Control: RadioButton (radio 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.