Android getting started tutorial (11)-comprehensive application of single option box radiogroup

Source: Internet
Author: User

Hello everyone, what we want to introduce today isRadiogroupGroup events.RadiogroupDifferentRadiobutton, Set to the sameRadioButton group, the sameRadiogroupThe buttons in the group can only make a single choice (single answer ).

First, designTextview widgetAndRadiogroupAndRadiogroupPut twoRadiobuttonIs not selected by default.ProgramRunning stage, usingOncheckedchangedAs the startup event device, let the user select one of the buttons to display the selected content.RadiobuttonThe option text is displayed onTextview.

 

Let's take a look:

 

 

 

The following are relatedCode:

 

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 guy </string>
<String name = "tr_radio_op2"> beauty </string>
<String name = "str_radio_question1"> are you? </String>

</Resources>

 

Main LayoutMain. 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"
>
<! -- First textview -->
<Textview
Android: Id = "@ + ID/mytextview"
Android: layout_width = "228px"
Android: layout_height = "49px"
Android: text = "@ string/str_radio_question1"
Android: textsize = "30sp"
/>
<! -- Create a radiogroup -->
<Radiogroup
Android: Id = "@ + ID/myradiogroup"
Android: layout_width = "137px"
Android: layout_height = "216px"
Android: Orientation = "vertical"
>
<! -- The First radiobutton -->
<Radiobutton
Android: Id = "@ + ID/myradiobutton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/tr_radio_op1"
/>
<! -- The second radiobutton -->
<Radiobutton
Android: Id = "@ + ID/myradiobutton2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/tr_radio_op2"
/>
</Radiogroup>

</Linearlayout>

 

Finally, the main control programRadiogroupdemo. 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, and radiobutton objects */
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 runs with oncheckedchangelistener */
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 ())
{
/* Upload the content of mradio1 to mtextview1 */
Mtextview1.settext (mradio1.gettext ());
}
Else if (checkedid = mradio2.getid ())
{
/* Upload the content of mradio2 to mtextview1 */
Mtextview1.settext (mradio2.gettext ());
}
}
};

}

 

RunRadiogroupdemo. Java, And the above results will be obtained... today we will be here ~

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.