2016-04-21 206 views
0

我想在射頻組上使用驗證。但我可以看到,如果沒有單選按鈕沒有檢查getCheckedRadioButtonId()的返回值不是-1。我正在使用數據綁定。RadioGroup getCheckedRadioButtonId()未經檢查的值未給出使用數據綁定正確android

無線電組的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> 

enter image description here

這裏是在截屏調試值。

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

您已在您的'xml'的'RadioGroup'中設置了'checkedButton'屬性。我想你會得到這個按鈕的ID。 – yennsarah

+0

不工作...你能舉一個例子嗎 –

+0

你做了什麼..?我沒有添加任何說明._。你有沒有刪除'android:checkedButton =「@ + id/sound」'行? – yennsarah

回答

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回來,即使沒有被選中。

+0

我會以爲OP會收到錯誤,因爲@ + id /聲音不是RadioGroups子項之一。它不應該是「@ + id/radio_unknown」還是radio_abnormal或radio_normal? – Martin

+0

他將checkedButton設置爲這個?顯然Android不關心。 – yennsarah