Android programming single option box Radiogroup Comprehensive application Example _android

Source: Internet
Author: User
Tags gettext

This example describes the Radiogroup usage of the Android Programming single option box. Share to everyone for your reference, specific as follows:

The group events for Radiogroup are described today. Radiogroup can be different radiobutton, limited to the same radio button group, the same Radiogroup group buttons, can only make a single choice (single selection).

First, we first design a TextView Widget, as well as a radiogroup, and place the Radiogroup within two RadioButton, default is not selected, in the program running phase, the use of oncheckedchanged As the startup event device, let user select one of the buttons to display the selected content, and the most RadioButton option text is displayed in the TextView.

Let's take a look at the effect chart:

The following are the relevant code involved:

String.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
 <string name= "Hello" >hello world, radiogroupdemo</string>
 <string name= "app_name" >RadioGroupDemo</string>
 <string Name= "TR_RADIO_OP1" > Handsome man </string>
 <string name= "TR_RADIO_OP2" > Beauty </string>
 <string Name= "Str_radio_question1" > Excuse me, are you? </string>
</resources>

Main layout Main.xml:

<?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 " > <!--the first TextView--> <textview android:id= "@+id/mytextview" android:layout_width= "228px" Android:layo ut_height= "49px" android:text= "@string/str_radio_question1" android:textsize= "30sp"/> <!--establish a radiogroup--&
 Gt <radiogroup android:id= "@+id/myradiogroup" android:layout_width= "137px" android:layout_height= "216px" Android:o rientation= "Vertical" > <!--the first RadioButton--> <radiobutton android:id= "@+id/myradiobutton1" Andro Id:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/tr_radio_op1"/> &L t;! --The second RadioButton--> <radiobutton android:id= "@+id/myradiobutton2" android:layout_width= "Wrap_content" an Droid:layout_height= "Wrap_content"Android:text=" @string/tr_radio_op2 "/> </RadioGroup> </LinearLayout> 

Finally, the main control program Radiogroupdemo.java:

Package com.android.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.RadioButton;
Import Android.widget.RadioGroup;
Import Android.widget.TextView;
 public class Radiogroupdemo extends activity {public TextView mTextView1;
 Public Radiogroup MRadioGroup1;
 Public RadioButton Mradio1,mradio2;
  public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.main);
  * * Obtain TextView, Radiogroup, RadioButton Object * * MTextView1 = (TextView) Findviewbyid (R.id.mytextview);
  MRadioGroup1 = (radiogroup) Findviewbyid (R.id.myradiogroup);
  MRadio1 = (RadioButton) Findviewbyid (R.id.myradiobutton1);
  MRadio2 = (RadioButton) Findviewbyid (R.id.myradiobutton2);
 /*radiogroup with Oncheckedchangelistener to run * * Mradiogroup1.setoncheckedchangelistener (Mchangeradio); 
  Private Radiogroup.oncheckedchangelistener Mchangeradio = new Radiogroup.oncheckedchangelistener () {@Override public void OnCheckedChanged (radiogrOUP Group, int checkedid) {//TODO auto-generated Method stub if (Checkedid==mradio1.getid ()) {/* MRadio1
   Content passed to mtextview1*/Mtextview1.settext (Mradio1.gettext ());
   else if (Checkedid==mradio2.getid ()) {/* MRadio2 the content to mtextview1*/Mtextview1.settext (Mradio2.gettext ());
}
  }
 };

 }

Run Radiogroupdemo.java, will get the above effect.

For more information on Android-related content readers can view the site: "Android View tips Summary", "Android Layout Layout Skills Summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course" , Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.