简体   繁体   English

Kotlin stdlib和数据绑定

[英]Kotlin stdlib and Databinding

Is it possible to use methods from Kotlin stdlib in xml? 是否可以在xml中使用Kotlin stdlib中的方法? For example this code 例如此代码

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

produce compilation error 产生编译错误

Execution failed for task ':app:compileDevDebugJavaWithJavac'. 任务':app:compileDevDebugJavaWithJavac'的执行失败。 java.lang.RuntimeException: Found data binding errors. java.lang.RuntimeException:发现数据绑定错误。 ****/ 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 **** **** /数据绑定错误**** msg:在类java.lang.String文件中找不到方法isNotEmpty():D:\\ Projects \\ PushTracker-Android \\ app \\ src \\ main \\ res \\ layout \\ fragment_appointment_simple_details。 xml loc:104:44-104:70 **** \\数据绑定错误****

It is obvious that databinding tries to find method isNotEmpty() in Java's String but can I force databinding compiler to use kotlin's String? 显然,数据绑定试图在Java的String中找到方法isNotEmpty() ,但是我可以强制数据绑定编译器使用kotlin的String吗?

"kotlin's String" does not exist. “ kotlin的字符串”不存在。 Kotlin's standard library defines extension methods to create the method you're referring to. Kotlin的标准库定义了扩展方法来创建您要引用的方法。 But since the data-binding library needs to generate Java code it cannot find the method you're referring to. 但是由于数据绑定库需要生成Java代码,因此无法找到您所引用的方法。

In order to use that method you will need to call it using the way Java would call it, which is as a static function: 为了使用该方法,您将需要使用Java调用它的方法来调用它,这是一个静态函数:

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

EDIT: this method is annotated with @InlineOnly , so this method might not exist outside of Kotlin code. 编辑:此方法用@InlineOnly注释,因此该方法可能不在Kotlin代码之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin中的数据绑定TypeConverter错误 - Databinding TypeConverter error in kotlin 在为 Android 工件生成的 POM 中排除 Kotlin stdlib 的 testImplementation 依赖项 - Excluding testImplementation dependency for Kotlin stdlib in generated POM for Android artifact Kotlin-android:未解决的参考数据绑定 - Kotlin-android: unresolved reference databinding Kotlin &amp; Databinding Int 值空检查问题 - Kotlin & Databinding Int value null check issue 找不到 arguments [org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50] 的方法 implementation() - Could not find method implementation() for arguments [org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50] 错误:找不到org.jetbrains.kotlin:kotlin-stdlib-jdk8 - ERROR: Could not find org.jetbrains.kotlin:kotlin-stdlib-jdk8 Gradle 同步问题:无法获取“kotlin-stdlib-jdk8-1.4.30.pom” - Gradle sync Problem : Could not GET “kotlin-stdlib-jdk8-1.4.30.pom” 更新到Android Studio 3.1后,我遇到此错误:找不到org.jetbrains.kotlin:kotlin-stdlib-jre8:1.2.0 - After update to Android Studio 3.1 I'm facing this error: Could not find org.jetbrains.kotlin:kotlin-stdlib-jre8:1.2.0 Android (Kotlin) 非字符串原始类型的双向数据绑定 - Android (Kotlin) Two-Way Databinding with non-string primitive type C stdlib函数的Java等效项 - Java equivalents for C stdlib functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM