1

我正在尝试实现课程 ViewModel 和数据绑定,在下面的 xml 中,我想使用TextUtils.isEmpty()方法来检查 editText 是否为空然后设置错误,当我搜索这个时我发现这个问题我在标签中添加了 importing TextUtils,但是我仍然无法从按钮onClick调用 editText "name_text"来检查它并设置错误

activity_main.xml

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="android.text.TextUtils"/>
        <import type="android.widget.EditText"/>
        <variable
            name="viewModel"
            type="com.anushka.roomdemo.SubscriberViewModel" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="15dp"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <EditText
            android:id="@+id/name_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_marginBottom="5dp"
            android:ems="10"
            android:hint="Subscriber's name"
            android:inputType="textPersonName"
            android:text="@={viewModel.inputName}"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/email_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:ems="10"
            android:hint="Subscriber's email"
            android:inputType="textEmailAddress"
            android:text="@={viewModel.inputEmail}"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/save_or_update_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="@{
                 ()->
               TextUtils.isEmpty(name_text.getText().toString())
                   ? name_text.setError("cannot be empty") 
                }"
                android:text="@={viewModel.saveOrUpdateButtonText}"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/clear_all_or_delete_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:onClick="@{()->viewModel.clearAll()}"
                android:text="@={viewModel.clearAllOrDeleteButtonText}"
                android:textSize="18sp"
                android:textStyle="bold" />


        </LinearLayout>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/subscriber_recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
    </LinearLayout>
</layout>

编辑:

我像这样编辑了onClick attr,xml中没有错误但我无法将消息传递给setError("Cannot be empty")方法

android:onClick="@{
                (nameText)-> TextUtils.isEmpty(nameText.text.toString())
                ? nameText.setError() : viewModel.saveOrUpdate()}"
4

2 回答 2

1

我强烈建议您避免将逻辑放入 XML 中。这应该放在可以测试它的视图模型中。

所以 xml 只是绑定到视图模型上的一个函数:

android:onClick="@{viewModel.saveOrUpdate()}"

以及名称字段上的错误状态:

    <EditText
        android:id="@+id/name_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:layout_marginBottom="5dp"
        android:ems="10"
        android:error="@{viewModel.inputNameError}" // <-----
        android:hint="Subscriber's name"
        android:inputType="textPersonName"
        android:text="@={viewModel.inputName}"
        android:textStyle="bold" />

然后您的视图模型处理逻辑:

fun saveOrUpdate {
    // Updating this field will set the value on the bound text field
    inputNameError = if (inputName.isNullOrEmpty()} "cannot be empty" else null
    // Do other stuff to save or update
}
于 2021-09-05T20:42:18.943 回答
0

在视图模型中为您的编辑文本创建 observablefield。您可以从 xml 将参数传递给它。

 android:onTextChanged="@{viewModel.editTextContent}

单击时只需检查它是否为空。 https://developer.android.com/reference/android/databinding/ObservableField

于 2021-09-05T14:49:10.617 回答