【发布时间】:2018-08-23 12:37:51
【问题描述】:
通过数据绑定,我正在设置文本字段的可见性。可见性取决于字符串是否为空或空或两者都没有。
<?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>
是否可以在数据元素中创建导入,以便我可以使用 kotlin.text.StringsKt 类中的 isNullOrBlank() 函数?
希望能够这样使用:android:visibility="@{(viewModel.data.title.isNullOrBlank() ? View.GONE : View.VISIBLE}"
【问题讨论】:
-
我怀疑
isNullOrBlank()是作为扩展函数实现的,我不确定数据绑定表达式解析器处理这些的能力如何。我怀疑是否有一种使用import的方法会有所帮助,因为它适用于类(以及接口和类似的东西),而不是函数。
标签: android kotlin android-databinding