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)