Android data binding, Radio Button not updating(Android数据绑定,单选按钮不更新)
问题描述
我有一些 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数据绑定,单选按钮不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android数据绑定,单选按钮不更新


- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01