Android数据绑定,单选按钮不更新

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)