gpt4 book ai didi

java - Kotlin stdlib 和数据绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 19:16:44 25 4
gpt4 key购买 nike

是否可以在 xml 中使用 Kotlin stdlib 中的方法?例如这段代码

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:textColor="#333333"
android:text="@{viewModel.note}"
android:visibility="@{viewModel.note.isNotEmpty ? View.VISIBLE : View.GONE}"/>

产生编译错误

Execution failed for task ':app:compileDevDebugJavaWithJavac'. java.lang.RuntimeException: Found data binding errors. ****/ data binding error ****msg:cannot find method isNotEmpty() in class java.lang.String file:D:\Projects\PushTracker-Android\app\src\main\res\layout\fragment_appointment_simple_details.xml loc:104:44 - 104:70 ****\ data binding error ****

很明显数据绑定(bind)试图在 Java 的字符串中找到方法 isNotEmpty() 但我可以强制数据绑定(bind)编译器使用 kotlin 的字符串吗?

最佳答案

“kotlin 的字符串”不存在。 Kotlin 的标准库定义了扩展方法来创建您所指的方法。但是由于数据绑定(bind)库需要生成 Java 代码,所以它找不到您所指的方法。

为了使用该方法,您需要使用 Java 调用它的方式调用它,即静态函数:

kotlin.text.StringsKt.isNotEmpty(viewModel.note)

编辑:此方法使用 @InlineOnly 注释,因此此方法可能不存在于 Kotlin 代码之外。

关于java - Kotlin stdlib 和数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43255482/

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