如果我有以下无线电组:
<RadioGroup
android:id="@+id/rgType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6" >
<RadioButton
android:id="@+id/rbBrides"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Bridge" />
<RadioButton
android:id="@+id/gbTunnel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tunnel" />
<RadioButton
android:id="@+id/rbHighway"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Highway" />
</RadioGroup>
我想做以下检查:
If (rgType is selected) {
system.out.println("ok");
}
if (rgType is void/null/not selected) {
system.out.println("choose at least one selection");
}
我可以使用用于个别单选按钮的isChecked()
,如下所示吗?
if (rgType.isChecked()) {
}
else {
}
答案 0 :(得分:2)
您可以使用getCheckedRadioButtonId查看是否已选中RadioButtons
之一。如果没有,可能会null
或更可能-1
,我不确定它会返回什么。无论哪种方式,运行它并查看它返回的内容。
我相信如果没有检查它会返回-1,所以试试
if (rgType == -1)
{
system.out.println("choose at least one selection");
}
else
{
system.out.println("ok");
}