本文介绍了Android数据绑定,Radio Button不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些RadioButton,我想让它们被检查/取消选中,因为模型的变化,使用数据绑定。

我设法设置一个EditText,它的工作正常。

无论如何,RadioButtons表现为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}
/>


解决方案

这是我目前为此做的: / p>

 < 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/>

这是骗子,因为我有一些代码逻辑给View ...但至少我不必在每个单独的Radiogroup上实现一个onchange监听器,我仍然可以发生绑定。



除非有人纠正我,并给我一个更多的伦理和专业解决方案我想我会坚持这个。


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}"
/>
解决方案

This is what I am currently doing for this:

        <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"/>

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数据绑定,Radio Button不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:19