Android数据绑定,单选按钮不更新
我有一些单选按钮,我希望在模型更改时使用数据绑定检查/取消检查它们。Android数据绑定,单选按钮不更新,android,android-layout,data-binding,radio-button,Android,Android Layout,Data Binding,Radio Button,我有一些单选按钮,我希望在模型更改时使用数据绑定检查/取消检查它们。 我成功地设置了一个EditText,它工作正常。 无论如何,单选按钮的行为就像android:checked属性不存在一样 <RadioButton android:id="@+id/radio_kitchen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@
我成功地设置了一个EditText,它工作正常。
无论如何,单选按钮的行为就像android:checked属性不存在一样
<RadioButton
android:id="@+id/radio_kitchen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kitchen"
android:checked="@{radiator.room==@string/kitchen?true:false}"
/>
这就是我目前为此所做的:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> car.setModel(@string/car_model_toyota)}"
android:checked="@{car.model.equals(@string/car_model_toyota)}"
android:text="@string/car_model_toyota"/>
这是作弊,因为我给了视图一些代码逻辑。。。但至少我不必在每一个radiogroup上实现onchange侦听器,而且我仍然可以实现绑定
除非有人纠正我,给我一个更加道德和专业的解决方案,否则我想我会坚持这个解决方案。这个答案可能会晚一些,但其他面临这个问题的人可以尝试: XML:
这是我在Android开发者身上找到的方法。您可以为每个单选按钮创建自定义ID,并使用radiogroup的Android:checkedButton通过双向数据绑定保留单选按钮的选中状态。 对于单选按钮的值,您可以使用android:onClick属性根据您的业务逻辑具有特定的值
<RadioGroup
android:checkedButton="@={signUpModel.genderId}"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/headerGender"
android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton
android:id="@+id/female"
android:onClick="@{()->signUpModel.setGender(@string/cd_female)}"
android:background="@drawable/bg_gender_cta_selector"
android:button="@drawable/ic_gender_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cd_female"/>
<RadioButton
android:id="@+id/male"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="@{()->signUpModel.setGender(@string/cd_male)}"
android:button="@drawable/ic_gender_male"
android:background="@drawable/bg_gender_cta_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cd_male"/>
</RadioGroup>
ids.xml(内部值)
您可能打算使用.equals而不是==我也尝试了.equals,但我不确定它是否受支持。这应该可以用:“@{散热器.room.equals(@string/kitchen)}”。如果没有,请提交一个bug,因为它应该可以工作。is散热器.room
返回字符串资源id或字符串?@GeorgeMount完美答案!我花了一个小时来分析这一点……你的答案中甚至没有使用数据绑定。
public void buttonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.iron_man:
if (checked)
// Iron man is the best!
break;
case R.id.batman:
if (checked)
// Batman rocks!
break;
}
}
<RadioGroup
android:checkedButton="@={signUpModel.genderId}"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/headerGender"
android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<RadioButton
android:id="@+id/female"
android:onClick="@{()->signUpModel.setGender(@string/cd_female)}"
android:background="@drawable/bg_gender_cta_selector"
android:button="@drawable/ic_gender_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cd_female"/>
<RadioButton
android:id="@+id/male"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="@{()->signUpModel.setGender(@string/cd_male)}"
android:button="@drawable/ic_gender_male"
android:background="@drawable/bg_gender_cta_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/cd_male"/>
</RadioGroup>
<resources>
<item name="female" type="id" format="string"/>
<item name="male" type="id" format="string"/>
</resources>