Application of Android Development--radiobutton

Source: Internet
Author: User

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

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.