使用Data Binding监听EditText文本的几种方法

前言

上一周系统的学习了Android Jetpack中的Data Binding,之前在网上查询监听EditText文本变化的方法的时候,有些Blog不知所云,因此打算总结一篇文章。想要成功的监听EditText文本的变化,主要有三种方法:

  1. 事件绑定
  2. BindingAdapter注解
  3. 双向绑定

我们一个一个的来讲解,先来介绍我们的登录界面:

<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>
        <!--需要的viewModel,通过mBinding.vm=mViewMode注入-->
        <variable
            name="model"
            type="com.joe.jetpackdemo.viewmodel.LoginModel"
            />

        <!-- 省略 -->
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <EditText
            android:id="@+id/et_account"
            android:text="@{model.n.get()}"
            android:onTextChanged="@{(text, start, before, count)->model.onNameChanged(text)}"
            ...
            />

        <EditText
            android:id="@+id/et_pwd"
            android:text="@{model.p.get()}"
            android:onTextChanged="@{model::onPwdChanged}"
            ...
            />
        <!-- TODO BindingAdapter app:addTextChangedListener="@{model.pwdWatcher}" -->


        <Button
            android:id="@+id/btn_login"
            .../>


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

以及LoginModel

class LoginModel constructor(name: String, pwd: String, context: Context):ViewModel() {
    val n = ObservableField<String>(name)
    val p = ObservableField<String>(pwd)
    var context: Context = context

    ...
}

1. 事件绑定

事件绑定可以分为方法引用和监听器绑定。

1.1 方法引用

我们以et_pwdEditText为例,看一下EditText监听文本的TextWatcher中的onTextChanged的方法:onTextChanged(s: CharSequence, start: Int, before: Int, count: Int)。如果是方法引用,要求引用的方法参数类型和返回类型要与要与监听的方法一致,因此我们的引用方法可以这么写:

    /**
     * 密码改变的回调函数
     */
    fun onPwdChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        p.set(s.toString())
    }

在布局文件中:

        <EditText
            android:id="@+id/et_pwd"
            android:text="@{model.p.get()}"
            android:onTextChanged="@{model::onPwdChanged}"
            ...
            />

1.2 监听器绑定

使用监听器绑定比方法引用随意一点,以布局文件中的et_accountEditText为例,返回类型一致即可,所以方法可以这么写:

    /**
     * 用户名改变回调的函数
     */
    fun onNameChanged(s: CharSequence) {
        n.set(s.toString())
    }

在布局文件中引用:

        <EditText
            android:id="@+id/et_account"
            android:text="@{model.n.get()}"
            android:onTextChanged="@{(text, start, before, count)->model.onNameChanged(text)}"
            ...
            />

2. 使用BindAdapter注解

使用BindingAdapter好处就是可以自定义属性,我们可以给EditText添加自定义的文本监听器,EditText是没有app:addTextChangedListener这个属性的:

    @BindingAdapter("addTextChangedListener")
    fun addTextChangedListener(editText: EditText, simpleWatcher: SimpleWatcher) {
        editText.addTextChangedListener(simpleWatcher)
    }

第一个参数必须是控件本身,使用:

        <EditText
            android:id="@+id/et_account"
            android:text="@{model.n.get()}"
            app:addTextChangedListener="@{model.nameWatcher}"
            ...
            />

3. 双向绑定

转载于:https://www.jianshu.com/p/c31e201718f4

猜你喜欢

转载自blog.csdn.net/weixin_33691700/article/details/91224030