The use of the RadioButton radio button for Android development

Source: Internet
Author: User

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

Related Article

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.