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数据绑定,单选按钮不更新
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
