【问题标题】:getCheckedRadioButtonId() on RadioGroup unchecked value not given correct using data binding android使用数据绑定android未正确给出RadioGroup上的getCheckedRadioButtonId()未经检查的值
【发布时间】: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
  • 请删除上述行。此外,您没有在帖子中提供任何 Viewid sound

标签: java android android-layout android-studio


【解决方案1】:

您在xml 中分配checkedButton 属性:

<RadioGroup
android:id="@+id/LabRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/sound">  //<--- remove this line

即使未检查任何内容,您也会收到此 id

【讨论】:

  • 我原以为 OP 会出现错误,因为 @+id/sound 不是 RadioGroups 的孩子之一。不应该是“@+id/radio_unknown”还是radio_abnormal或者radio_normal?
  • 他把checkedButton设置成了这个?显然 Android 不在乎。
猜你喜欢
  • 2016-01-20
  • 1970-01-01
  • 2018-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2010-10-14
  • 2019-11-30
相关资源
最近更新 更多