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: