问题描述
我有一些 RadioButtons,我希望它们在模型更改时被选中/取消选中,使用数据绑定.
我设法设置了一个 EditText,它工作正常.
无论如何,RadioButtons 的行为就像 android:checked 属性不存在一样.
I have some RadioButtons and I want them to be checked/unchecked as the model changes, using Data Binding.
I managed to set up an EditText and it is working fine.
Anyway, the RadioButtons behave as the android:checked property wouldn't be there.
<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"/>
这是作弊,因为我在某种程度上为视图提供了一些代码逻辑...但至少我不必在每个无线电组上实现 onchange 侦听器,而且我仍然可以进行绑定...
It's cheating since I am kind of giving some code logic to the View... but at least I don't have to implement an onchange listener on every single radiogroup and I still get the binding to happen...
除非有人纠正我并给我一个更合乎道德和专业的解决方案,否则我想我会坚持这个.
Unless someone correct me and gives me a more ethic and professional solution I think I am going to stick with this one.
这篇关于Android数据绑定,单选按钮不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!