【发布时间】:2016-04-21 08:24:28
【问题描述】:
我正在尝试对无线电组使用验证。但我可以看到如果没有选中单选按钮,则 getCheckedRadioButtonId() 的返回值不是-1。我正在使用数据绑定。
电台组的xml文件。
<RadioGroup
android:id="@+id/LabRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/sound">
<RadioButton
android:id="@+id/radio_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{Exams.result.equals(@string/Exam_Normal)?true:false}"
android:text="Normal" />
<RadioButton
android:id="@+id/radio_abnormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{Exams.result.equals(@string/Exam_Abnormal)?true:false}"
android:text="Abnormal" />
<RadioButton
android:id="@+id/radio_unknown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="@{Exams.result.equals(@string/Exam_unknown)?true:false}"
android:text="Unknown" />
</RadioGroup>
这是屏幕截图中的调试值。
Java 代码
private boolean Validation() {
int radiogroup = binding.LabRadioGroup.getCheckedRadioButtonId();
if (binding.LabRadioGroup.getCheckedRadioButtonId() == -1) {
binding.radioNormal.setError(CommonMessages.GROUP_RADIO_ERROR_TAG);
return false;
}
return true;
}
【问题讨论】:
-
您已在您的
RadioGroup中设置checkedButton属性在您的xml中。我想你得到了这个按钮的 ID。 -
不工作...你能举个例子吗
-
你做了什么..?我没有添加任何指令._。您是否删除了
android:checkedButton="@+id/sound"行? -
不,我没有……我试过 int i = binding.sound.getCheckedRadioButtonId();得到一个错误为 Error:(238, 15) error: cannot find symbol variable sound
-
请删除上述行。此外,您没有在帖子中提供任何
View和idsound。
标签: java android android-layout android-studio