RadioGroup is used to manage a set of Radio buttons. RadioButton is a single-choice Button. Only one RadioButton in the same group can be selected. In the preceding example, the Android ApiDemos sample is parsed (107): Views-> Controls-> 1. Light Theme.
Using RadioGroup is very simple. In XML, use RadioGroup as the Container of RadioButton:
<RadioGroup
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
Android: checkedButton = "@ + id/lunch"
Android: id = "@ + id/menu">
<RadioButton
Android: text = "@ string/radio_group_1_breakfast"
Android: id = "@ + id/breakfast"
/>
<RadioButton
Android: text = "@ string/radio_group_1_lunch"
Android: id = "@ id/lunch"/>
<RadioButton
Android: text = "@ string/radio_group_1_dinner"
Android: id = "@ + id/dinner"/>
<RadioButton
Android: text = "@ string/radio_group_typesall"
Android: id = "@ + id/all"/>
<TextView
Android: text = "@ string/radio_group_1_selection"
Android: id = "@ + id/choice"/>
</RadioGroup>
In the code, you can dynamically Add a View (such as RadioButton) to a RadioGroup:
[Java]
// Test adding a radio button programmatically
RadioButton newRadioButton = new RadioButton (this );
NewRadioButton. setText (R. string. radio_group_snack );
NewRadioButton. setId (R. id. snack );
LinearLayout. LayoutParams layoutParams
= New RadioGroup. LayoutParams (
RadioGroup. LayoutParams. WRAP_CONTENT,
RadioGroup. LayoutParams. WRAP_CONTENT );
MRadioGroup. addView (newRadioButton, 0, layoutParams );
// Test adding a radio button programmatically
RadioButton newRadioButton = new RadioButton (this );
NewRadioButton. setText (R. string. radio_group_snack );
NewRadioButton. setId (R. id. snack );
LinearLayout. LayoutParams layoutParams
= New RadioGroup. LayoutParams (
RadioGroup. LayoutParams. WRAP_CONTENT,
RadioGroup. LayoutParams. WRAP_CONTENT );
MRadioGroup. addView (newRadioButton, 0, layoutParams );