Android------radiobutton radio button

Source: Internet
Author: User
<span id="Label3"></p><p><p>Mainactivity.java:</p></p><p><p><br></p></p><p><p><br></p></p><p><p></p></p><pre code_snippet_id="645894" snippet_file_name="blog_20150416_1_3351526" name="code" class="html">Import Android.app.activity;import android.os.bundle;import Android.view.menu;import Android.view.MenuItem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.compoundbutton;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.toast;import android.widget.compoundbutton.oncheckedchangelistener;/** * RadioButton * @author Caesar * */public class Mainactivity extends Activity implements Onclicklistener{private Button button;private radiogroup group; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.activity_main); Group = (radiogroup) This.findviewbyid (r.id.sex); button = (button) This.findviewbyid ( r.id.button); Button.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated Method stub//gets The number of options for a radio button group int len = Group.getchildcount (); String msgsstring= ""; for (int i = 0; I < Len;i++) {RadioButton RadioButton = (radioButton) Group.getchildat (i), if (radiobutton.ischecked ()) {msgsstring = Radiobutton.gettext (). ToString (); break;}} Toast.maketext (mainactivity.this, msgsstring, 1). show ();}}</pre><br><br><p><p></p></p><p><p>Activity_main.xml:</p></p><p><p><br></p></p><p><p></p></p><pre code_snippet_id="645894" snippet_file_name="blog_20150416_2_347976" name="code" class="html"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:orienta tion= "vertical" android:paddingbottom= "@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.example.radiobuttondemo.MainActivity "> <textview android:layout_wi Dth= "fill_parent" android:layout_height= "wrap_content" android:text= "sex"/> <radiogroup Android oid:id= "@+id/sex" android:layout_width= "wrap_content" android:layout_height= "wrap_content" > <R Adiobutton android:id= "@+id/boy" android:layout_width= "wrap_content" Android:layout_heigh t= "wrap_content" android:text= "male"/><radiobutton android:id= "@+id/girl" android:layout_width= "wrap_content" android:layout _height= "wrap_content" android:text= "female"/> </RadioGroup> <button android:id= "@+id/butt On "android:layout_width=" fill_parent "android:layout_height=" wrap_content "android:text=" Select Gender "/> </LinearLayout></pre><br><br><p><p></p></p><p><p><br></p></p><p><p>Android------radiobutton radio button</p></p></span>

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.