【问题标题】:"DataBinding" How to check if editText isEmpty and setError from onClick of the button in xml“DataBinding”如何检查 xml 中按钮的 onClick 是否为 editText isEmpty 和 setError
【发布时间】:2021-11-02 22:16:39
【问题描述】:

我正在尝试实现课程 ViewModel 和数据绑定,在下面的 xml 中,我想使用 TextUtils.isEmpty() 方法来检查 editText 是否为空然后设置错误,在我搜索这个时我发现了这个 question 我添加了在标签中导入 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()}"

【问题讨论】:

    标签: android data-binding android-xml android-databinding android-binding-adapter


    【解决方案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
    }
    

    【讨论】:

    【解决方案2】:

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

     android:onTextChanged="@{viewModel.editTextContent}
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 2021-11-30
      • 1970-01-01
      • 2021-01-25
      相关资源
      最近更新 更多