Android ApiDemos示例解析(186):Views->Radio Group

RadioGroup 用来管理一组Radio Button,RadioButton 为单选钮,同一组中的RadioButton只能有一个选中。前面的例子有Android ApiDemos示例解析(107):Views->Controls->1. Light Theme

使用RadioGroup非常简单,在XML中,将RadioGroup作为RadioButton的Container即可:

<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_1_all”
android:id=”@+id/all” />
<TextView
android:text=”@string/radio_group_1_selection”
android:id=”@+id/choice” />
</RadioGroup>

在代码中也可以动态将一个View (如RadioButton)动态添加到一个RadioGroup中:

// 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);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值