Ching
Original address: http://blog.csdn.net/qingdujun/article/details/40679959
This article demonstrates the use of the Android radio button.
1) Mainactivity.java
Package Com.dg.radio;import Android.app.activity;import Android.os.bundle;import android.view.menu;import Android.view.menuitem;import Android.widget.radiogroup;import Android.widget.RadioGroup.OnCheckedChangeListener; Import Android.widget.toast;public class Mainactivity extends Activity {radiogroup rgroup; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Gets the Radiogroup component on the interface Rgroup = (radiogroup) Findviewbyid (r.id.radiogroup1); Rgroup.setoncheckedchangelistener (New Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (Radiogroup arg0, int arg1) {//TODO auto-generated method Stubswitch (arg1) {case R.id.male:toast.maketext (Getapplicationcontext (), "The gender of your choice is male", 0). Show (); Break;case R.id.female:toast.maketext (Getapplicationcontext (), "The sex you choose is female", 1). Show (); break; Default:break;}}); } @Override public Boolean oncreateoptionsmenu (Menu meNU) {//Inflate the menu; This adds items to the action bar if it is present. Getmenuinflater (). Inflate (R.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 (); if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); }}
2) Activity_main.xml
<relativelayout 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:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.dg.radio.MainActivity" > <radiogroup android:id= "@+id/radiogroup1" Android:layout_widt H= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" Android:la Yout_centerhorizontal= "true" android:layout_margintop= "134DP" ><!--Default selection: Male-to-<radiobutton Android:id= "@+id/male" android:layout_width= "wrap_content" android:layout_height= "Wrap_conten T "android:checked=" true "Android:text= "Male"/> <radiobutton android:id= "@+id/female" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "female"/> </RadioGroup></RelativeLayout>
3) Operation effect
Original address: http://blog.csdn.net/qingdujun/article/details/40679959
The use of the RadioButton radio button for Android development