当前位置:操作系统 > 安卓/Android >>

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中:


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

 

 \

 


 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,