我正在尝试在无线电组上使用验证。但我可以看到如果没有选中单选按钮,则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;
}
答案 0 :(得分:1)
您正在checkedButton
中分配xml
属性:
<RadioGroup
android:id="@+id/LabRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/sound"> //<--- remove this line
即使没有选中任何内容,您也会收到此id
。