题
我试图用问题制作一个应用程序,每个问题都有4个答案选项(多项选择),我使用了4个答案的4个单选按钮。有没有办法将它们加入放射组中,或者我应该单独处理每个单选按钮吗?无线电组只有3个单选按钮,我可以将单选按钮增加到三个以上吗?如果是,那么如何?
解决方案
您可以从代码中获取每个radiobutton:
RadioGroup rg = (RadioGroup )findViewById(R.id.radio_group);
RadioButton r1 = (RadioButton) rg.getChildAt(0);
RadioButton r2 = (RadioButton) rg.getChildAt(1);
RadioButton r3 = (RadioButton) rg.getChildAt(2);
RadioButton r4 = (RadioButton) rg.getChildAt(3);
.
是的,您可以在rador group
中添加更多的3个单选按钮<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
. 其他提示
您必须将单选按钮添加到RadioGroup,然后将RadioGroup添加到布局中。
final RadioButton[] rb = new RadioButton[4];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<4; i++){
rb[i] = new RadioButton(this);
rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
rb[i].setText("Test");
}
ll.addView(rg);//you add the whole RadioGroup to the layout
ll.addView(submit);
.
编辑:
或者您可以在XML中定义RadioGroup:
<TableRow>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/radiobuttons">
</RadioGroup>
</TableRow>
.
然后编程添加一个附加按钮:
RadioGroup rg = (RadioGroup) findViewById(R.id.radiobuttons);//not this RadioGroup rg = new RadioGroup(this);
rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
for(int i=0; i<4; i++)
{
rb[i] = new RadioButton(this);
rg.addView(rb[i]);
rb[i].setText("Test");
}
. 不隶属于 StackOverflow