1. Introduction
RadioButton is a radio button, when a button is selected, tapping another button does not make the previous button unchecked. Radiogroup is a container that can hold multiple radiobutton,
By using Radiogroup, you can implement only one at a time when the button is selected.
2. Build
We can select the radio button from the widgets to build the interface shown in Figure two.
XML code:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/relativelayout1"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >
<textview
android:id= "@+id/textview"
android:layout_margintop= "10DP"
android:layout_width= "Match_parent"
android:layout_height= "250DP"
android:text= "@string/tx" >
</TextView>
<radiogroup
android:id= "@+id/rg"
android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:orientation= "vertical" >
<radiobutton
android:id= "@+id/radiobutton1"
android:layout_width= "Fill_parent"
android:layout_height= "100DP"
android:text= "@string/rb1"/>
<radiobutton
android:id= "@+id/radiobutton2"
android:layout_width= "Fill_parent"
android:layout_height= "74DP"
android:text= "@string/rb2"/>
</RadioGroup>
</RelativeLayout>
3. Code
public class Activity1 extends Activity {
Private RadioButton Rbutton1;
Private RadioButton Rbutton2;
Private Radiogroup Rgroup;
Private TextView TV;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACT1);
Rbutton1 = (RadioButton) Findviewbyid (R.id.radiobutton1);
Rbutton2 = (RadioButton) Findviewbyid (R.id.radiobutton2);
Rgroup = (radiogroup) Findviewbyid (R.ID.RG);
TV = (TextView) Findviewbyid (R.id.textview);
//Set listener for Radiogroup
Rgroup.setoncheckedchangelistener (New Oncheckedchangelistener () {
public void OnCheckedChanged (radiogroup group, int checkedid) {
if (Checkedid = = Rbutton1.getid ()) {
Displaytoast ("You have chosen" + rbutton1.gettext ());
} else {
Displaytoast ("You have chosen" + rbutton2.gettext ());
}
}
});
}
//Custom location toast
public void Displaytoast (string string) {
//Last item indicates time of content display
Toast mtoast = Toast.maketext (Getapplicationcontext (), String, Toast.length_long);
//content is displayed at the bottom, the previous item represents move left and right, and the latter indicates move up or down
Mtoast.setgravity (gravity.bottom, 0, 200);
Mtoast.show ();
}
4. Effects
Application of Android Development--radiobutton