Android UI Control Series: RadioButton (radio button)

Source: Internet
Author: User
Radio button RadioButton on the Android platform is also a lot of applications, such as some options, the use of radio buttons, the implementation of the radio button is composed of two parts, that is, RadioButton and Radiogroup in conjunction with.

RadioButton radio Button;

Radiogroup is a single-selection combo box used to frame the RadioButton;

In the absence of Radiogroup, RadioButton can all be selected;

When multiple RadioButton are radiogroup included, RadioButton can only select one;

Note: Radio button event monitoring uses Setoncheckedchangelistener to listen for radio buttons

Example:

A choice problem, choose which city beauty most, of course, this is to test

Radiotest.java

Package Org.loulijun.radio;import Android.app.activity;import Android.os.bundle;import android.view.Gravity;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.textview;import        Android.widget.toast;public class Radiotest extends activity {/** Called when the activity is first created. */        TextView TextView;        Radiogroup Radiogroup;    RadioButton Radio1,radio2,radio3,radio4;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);        textview= (TextView) Findviewbyid (R.ID.TEXTVIEW1);        radiogroup= (Radiogroup) Findviewbyid (r.id.radiogroup1);        radio1= (RadioButton) Findviewbyid (R.id.radiobutton1);        Radio2= (RadioButton) Findviewbyid (R.id.radiobutton2);        radio3= (RadioButton) Findviewbyid (R.id.radiobutton3);        radio4= (RadioButton) Findviewbyid (R.ID.RADIOBUTTON4); Radiogroup.setoncheckedchangelistener (New Radiogroup.onCheckedchangelistener () {@Override public void oncheckedchanged (Radiogroup Group, int checkedid) {//TODO auto-generated method Stub I F (Checkedid==radio2.getid ()) {displaytoast ("correct answer:" + Radio2.gettext () + ", Congratulations, answer right!"                                "); }else {displaytoast ("Please note that the answer is wrong!                                ");    }                        }                });            } public void Displaytoast (String str) {Toast toast=toast.maketext (this, str, toast.length_long);            Toast.setgravity (gravity.top,0,220);    Toast.show (); }}

Strings.xml file

<?xml version= "1.0" encoding= "Utf-8"?>  <resources>      <string name= "Hello" > which city is more beautiful? </string>      <string name= "app_name" > Radio button Test </string>      <string name= "RadioButton1" > Hangzhou </string>      <string name= "RadioButton2" > Chengdu </string>      <string name= "RadioButton3" > Chongqing </string>      <string name= "RadioButton4" > Suzhou </string>  </resources>

Main.xml file: Note that inside this, 4 RadioButton are included in the Radiogroup

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/h Ello "android:id=" @+id/textview1 "/> <radiogroup android:id=" @+id/radiogroup1 "Androi            D:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "vertical"                    android:layout_x= "3px" > <radiobutton android:id= "@+id/radiobutton1"                    Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/radiobutton1"/> <radiobutton android:id= "@+id/rad Iobutton2 "AndroId:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@s                    Tring/radiobutton2 "/> <radiobutton android:id=" @+id/radiobutton3 "                    Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "@string/radiobutton3"/> <radiobutton android:id= "@+id/radiob                    Utton4 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " android:text= "@string/radiobutton4"/> </RadioGroup></LinearLayout>

The results of the operation are as follows:

If we choose Hangzhou, it will prompt the wrong toast

When you check Chengdu again, you will be prompted with the correct answer


Here can be seen, the use of radio buttons, if only use RadioButton, the configuration file Radiogroup removed, of course, to re-set for each radio button monitoring, so that the RadioButton is no different from the button, We can select multiple, so be aware that radio buttons are used with Radiogroup to enable single-selection functionality.

The above is the Android UI Control series: RadioButton (radio button) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • 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.