gpt4 book ai didi

可绘制对象上的 Android 数据绑定(bind)

转载 作者:太空狗 更新时间:2023-10-29 15:33:10 25 4
gpt4 key购买 nike

我有两个形状可绘制对象,rounded_corners.xml 和 rounded_corners_red.xml,它们将分别用于显示有效文本输入和无效文本输入。

我希望在用户单击登录按钮时动态设置此 drwable,这样如果有效文本显示 rounded_corners.xml,如果无效则显示 rounded_corners_red.xml。

下面是我如何将它放入我的布局 xml 中。

<EditText android:id="@+id/et_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@={systemSettings.isValid ? @drawable/rounded_corners : @drawable/rounded_corners_red}"
android:text="@={systemSettings.serverIP, default=@string/ip_host}"
android:textColor="#000000" />

我希望根据我的模型类中定义的 isValid 可观察变量动态应用可绘制对象。我的代码编译没有错误。但它给出了运行时错误

java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:The expression ((systemSettingsIsValidGet) ? (getDrawableFromResource(etIp, R.drawable.rounded_corners)) : (getDrawableFromResource(etIp, R.drawable.rounded_corners_red))) cannot cannot be inverted: The condition of a ternary operator must be constant: android.databinding.tool.writer.KCode@429a75fd
file:D:xxx\app\src\main\res\layout\fragment_system_settings.xml
loc:92:47 - 92:128
****\ data binding error ****

有人知道为什么会这样吗?谢谢。

最佳答案

您的声明是双向绑定(bind) @={}

@={systemSettings.isValid ? @drawable/rounded_corners : @drawable/rounded_corners_red}`

这就是为什么你会发现错误指出表达式

cannot cannot be inverted

它甚至直接给你原因:

The condition of a ternary operator must be constant

但由于您只是获取可绘制资源,因此只需从表达式中删除 = 即可。

关于可绘制对象上的 Android 数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45680264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com