通过数据绑定,我可以设置文本字段的可见性。可见性取决于字符串为null或为空,或两者都不为。
<?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>
Run Code Online (Sandbox Code Playgroud)
是否可以在数据元素中创建导入,以便可以使用类中的isNullOrBlank()函数kotlin.text.StringsKt?
希望能够像这样使用它: android:visibility="@{(viewModel.data.title.isNullOrBlank() ? View.GONE : View.VISIBLE}"