数据绑定:视图到视图绑定,无需 java 代码

Data binding: View to view binding, without java code

在我的布局中,我有一个复选框,它应该切换不同视图的启用状态。这意味着,一个视图的选中值应该绑定到不同视图的启用值。

对我来说,这听起来像是新数据绑定库的完美用例,在我看来这应该是 xml 中的简单行,而不必使用 Java 代码。我在想 android:enabled="@{(@id/chk_enabled).isChecked()}" 之类的东西。很遗憾,这是无效的,我找不到任何关于如何正确执行此操作的信息。

这就是为什么我想知道,这可能吗?

试试这个:

<CheckBox
            android:id="@+id/showMore"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

            <EditText
                android:enabled="@{showMore.checked ?true:false}"
                android:layout_width="match_parent"
                android:layout_height="20dp" />