繁体   English   中英

使用数据绑定时在 xml 中找不到属性“android:onTextChanged”

[英]Cannot find attribute "android:onTextChanged" in xml when using data binding

将数据绑定到我的片段 xml 时,我在 EditText 中找不到属性android:onTextChanged 我已经搜索了答案, 这个答案说它是开箱即用的。 我已经设定

data-binding { enable = true }

在我的 build.gradle 中,我当前的构建 gradle 是 3.5.3。

这是我的 xml 的一部分:

<layout>

    <data>

        <variable
            name="vm"
            type=".ViewModel" />
    </data>

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".screens.hangouts.create_hangout_new.Step1FragmentNew">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_category"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tv_hangout_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/rv_category" />

            <EditText
                android:id="@+id/et_hangout_name"
                android:background="@drawable/bkg_stroke_red"
                android:layout_width="match_parent"
                android:layout_height="30dp"
                app:layout_constraintTop_toBottomOf="@id/tv_hangout_name" />
...

您可以使用

 android:afterTextChanged="@{(editable)->viewModel.afterEmailTextChanged(editable)}"

在您的视图模型中设置函数

public void afterEmailTextChanged(CharSequence s) {
    user.setEmail(s.toString());
}

在您的 xml 文件中:

<EditText
   android:id="@+id/et"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:textChangedListener="@{model.textWatcher}"  />  

在您的模型类中这样的代码中:

public class Model{
private TextWatcher textWatcher;

public Model(){
        this.textWatcher= getTextWatcherIns();
}

private TextWatcher getTextWatcherIns() {
        return new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //do some thing
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //do some thing

            }

            @Override
            public void afterTextChanged(Editable s) {
                //do some thing
            }
        };
    }

    public TextWatcher getTextWatcher() {
        return textWatcher;
    }

    public void setTextWatcher(TextWatcher textWatcher) {
        this.textWatcher = textWatcher;
    }

    @BindingAdapter("textChangedListener")
    public static void bindTextWatcher(EditText editText, TextWatcher textWatcher) {
        editText.addTextChangedListener(textWatcher);
    }
}

有关它的更多信息,请阅读此文档。 还包括在您的答案app:textChangedListenerEditText并尝试。

我希望它会帮助你...!

如果您使用如下数据绑定,则可以使用2 向绑定

<EditText
            android:id="@+id/et_hangout_name"
            android:background="@drawable/bkg_stroke_red"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:text="@={viewmodel.textChangedMutableLiveData}"
            app:layout_constraintTop_toBottomOf="@id/tv_hangout_name" />

然后在你的视图模型中,

var textChangedMutableLiveData = MutableLiveData<String>()

var textChangedLiveData = textChangedMutableLiveData.map {
    // here you get the changed text every time user types something. now you can observe textChangedLiveData in your activity and do whatever you wanna do when text changes. 
}

您需要定义一个BindingAdapter才能在 XML 中使用它,例如android:afterTextChanged

定义项目中的某处

@BindingAdapter("android:afterTextChanged")
public static void setListener(TextView view, TextViewBindingAdapter.AfterTextChanged after) {
        setListener(view,after);
    }

代码在此处参考: 此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM