Android tutorial (8) -- Use of the radio button RadioButton Control

Source: Internet
Author: User

Now, let's talk about the use of the single-choice button RadioButton control.

Although a single sequence is not as common as multiple selection boxes, it is still required in many cases.

Now let's take a look at how it works.

Step 1: Create a project Ep. Radio without renaming the view and activity.

Step 2: design the view in a simple way (after the previous study, I believe that you know which file the view refers to. That's right, activity_main.xml ):


[Java]
<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: 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 = ". MainActivity">
 
<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "what are you? "/>
 
<RadioGroup
Android: id = "@ + id/radioGroup1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "19dp">
 
<RadioButton
Android: id = "@ + id/radio0"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "handsome guy"/>
 
<RadioButton
Android: id = "@ + id/radio1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "beauty"/>
 
</RadioGroup>
 
</RelativeLayout>

<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: 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 = ". MainActivity">

<TextView
Android: id = "@ + id/textView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "what are you? "/>

<RadioGroup
Android: id = "@ + id/radioGroup1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignLeft = "@ + id/textView1"
Android: layout_below = "@ + id/textView1"
Android: layout_marginTop = "19dp">

<RadioButton
Android: id = "@ + id/radio0"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "handsome guy"/>

<RadioButton
Android: id = "@ + id/radio1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "beauty"/>

</RadioGroup>

</RelativeLayout>
You can change the view by yourself, but it does not have to be designed like me.

Remember that only one of the single-choice buttons can be selected, but one page can have multiple groups.

Step 3: Write the core file MainActivity. java (the core file is the. java file. Remember this ):


[Java]
Package com. example. ep. radio;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. widget. RadioButton;
Import android. widget. RadioGroup;
Import android. widget. TextView;
 
Public class MainActivity extends Activity {
 
Private TextView TV;
Private RadioButton rd1, rd2;
Private RadioGroup rdg;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

TV = (TextView) findViewById (R. id. textView1 );
Rd1 = (RadioButton) findViewById (R. id. radio0 );
Rd2 = (RadioButton) findViewById (R. id. radio1 );
Rdg = (RadioGroup) findViewById (R. id. radioGroup1 );

Rdg. setOnCheckedChangeListener (rdgcc );
}

Private RadioGroup. OnCheckedChangeListener rdgcc = new RadioGroup. OnCheckedChangeListener (){

@ Override
Public void onCheckedChanged (RadioGroup group, int checkedId ){
// TODO Auto-generated method stub
If (checkedId = rd1.getId ()){
TV. setText (rd1.getText ());
} Else if (checkedId = rd2.getId ()){
TV. setText (rd2.getText ());
}
}
};
 
}

Package com. example. ep. radio;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. widget. RadioButton;
Import android. widget. RadioGroup;
Import android. widget. TextView;

Public class MainActivity extends Activity {

Private TextView TV;
Private RadioButton rd1, rd2;
Private RadioGroup rdg;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

TV = (TextView) findViewById (R. id. textView1 );
Rd1 = (RadioButton) findViewById (R. id. radio0 );
Rd2 = (RadioButton) findViewById (R. id. radio1 );
Rdg = (RadioGroup) findViewById (R. id. radioGroup1 );

Rdg. setOnCheckedChangeListener (rdgcc );
}
 
Private RadioGroup. OnCheckedChangeListener rdgcc = new RadioGroup. OnCheckedChangeListener (){

@ Override
Public void onCheckedChanged (RadioGroup group, int checkedId ){
// TODO Auto-generated method stub
If (checkedId = rd1.getId ()){
TV. setText (rd1.getText ());
} Else if (checkedId = rd2.getId ()){
TV. setText (rd2.getText ());
}
}
};

}

Finally, we will test the result as follows:

 



 

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.