First, Radiogroup and RadioButton
1. What is Radiogroup:
A collection of RadioButton that provides a multi-select mechanism
2. What is RadioButton:
RadioButton wrapped in Radiogroup, Radiogroup represents a group of RadioButton, the following can have a lot of RadioButton, but only one is selected
3. Radiogroup Properties:
android:orientation--set the form of RadioButton in Radiogroup (with two values: horizontal (horizontal arrangement) and vertical (vertical arrangement))
Second, the code shows:
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"android:orientation= "Horizontal" > <RadiogroupAndroid:id= "@+id/radiogroup1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <RadioButtonAndroid:id= "@+id/radio0"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:checked= "true"Android:text= "Male" /> <RadioButtonAndroid:id= "@+id/radio1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "female" /> </Radiogroup></LinearLayout>
PackageCom.muke.textview_edittext;ImportAndroid.os.Bundle;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.RadioGroup.OnCheckedChangeListener;Importandroid.app.Activity; Public classMainactivityextendsActivityImplementsoncheckedchangelistener{PrivateRadiogroup RG; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //First step: Initialize the control (find the control you want to manipulate)RG =(Radiogroup) Findviewbyid (r.id.radiogroup1); //Step Two: Implement Radiogroup monitoring EventsRg.setoncheckedchangelistener ( This); } @Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) { //Checkedid indicates the ID of the radiobutton that was selected Switch(checkedid) { CaseR.id.radio0:system.out.println ("Male is selected"); Break; CaseR.id.radio1:system.out.println ("Female is selected"); Break; } } }
Android Control Radiogroup and RadioButton (radio control)