Via data-binding I am setting the visibility of a text field. The visibility is depending on a string being null or empty or nothing of the both.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <import type="android.view.View"/>

        <variable
            name="viewModel"
            type="com.example.viewModel"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        <TextView
            android:id="@+id/textField1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{viewModel.data.text}"
            android:visibility="@{(viewModel.data.text == null || viewModel.data.text.empty) ? View.GONE : View.VISIBLE}"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/> 

    </android.support.constraint.ConstraintLayout>

Is it possible to create an import in the data element so that I can use the isNullOrBlank() function from the kotlin.text.StringsKt class?

It was hoping to be able to use it like this: android:visibility="@{(viewModel.data.title.isNullOrBlank() ? View.GONE : View.VISIBLE}"

推荐答案

Android数据绑定仍然从XML而不是Kotlin代码生成Java代码,一旦数据绑定将迁移到生成Kotlin代码而不是Java,我相信我们将能够在XML中使用Kotlin扩展函数,这将非常酷.

我相信,随着谷歌大力推进Kotlin ,这种情况很快就会发生.但就目前而言,你必须做到以下几点

TextUtils.isEmpty() as mentioned by @Uli Don't forget to write an import.

你不能使用StringKt的原因.xml中的isNullOrBlack:

Below is the code from Kotlin String.kt

@kotlin.internal.InlineOnly
public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
        returns(false) implies (this@isNullOrEmpty != null)
    }

    return this == null || this.length == 0
}

As you can see it is annotated with @kotlin.internal.InlineOnly which says the Java generated code of this method will be private.

InlineOnly表示与此Kotline对应的Java方法 函数被标记为私有,因此Java代码无法访问它( 是调用内联函数而不进行实际内联的唯一方式. IT).

这意味着不能从Java调用它,而且由于数据绑定生成的代码是在Java中生成的,所以它也不能用于数据绑定.经验法则是您可以从Java访问的内容,我想说,如果不只是使用旧的Java方式,您可以在数据绑定中使用它.

Kotlin相关问答推荐

Fleet无法从构建文件自动重新加载更改错误'

Regex(Kotlin)仅匹配句子末尾的句号,而忽略中间的句号,如缩写

Kotlin编译器如何决定是否可以在任何给定点调用Suspend方法?

为什么不';Kotlin是否在数据类构造函数中隐式分配null值可为null的字段?

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

每个 Kotlin 版本的默认 Kotlin 语言版本是什么?

将 SharedPreferences 中的值公开为流

在 Kotlin 中,我可以在集合上有一个条件构建器元素吗?

Kotlin:伴随对象内的函数扩展

正则表达式 FindAll 不打印结果 Kotlin

奇怪的 cotlin check Not Null 参数错误

在 SplashActivity 中显示的 Firebase 应用内消息.如何在 MainActivity 中显示它?

无法删除部分子项.这可能是因为进程打开了文件或将其工作目录设置在目标目录中

在粘贴时将 java 转换为 kotlin

Kotlin 中的内联构造函数是什么?

使用范围的稀疏sparse值列表

Kotlin:如何从另一个类访问字段?

旋转 kotlin 数组

在 IntelliJ Idea 中未为 Kotlin @ConfigurationProperties 类生成 spring-configuration-metadata.json 文件

如何在 firebase 数据库中使用 kotlin 协程