简体   繁体   English

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

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

I can't find attribute android:onTextChanged in EditText when implement data-binding to my fragment xml.将数据绑定到我的片段 xml 时,我在 EditText 中找不到属性android:onTextChanged I have searched for the answer, and this one said it is supported out of the box.我已经搜索了答案, 这个答案说它是开箱即用的。 I have set我已经设定

data-binding { enable = true }

in my build.gradle, and my current build gradle is 3.5.3.在我的 build.gradle 中,我当前的构建 gradle 是 3.5.3。

Here is a part of my xml:这是我的 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" />
...

You can use您可以使用

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

set Function in your viewmodel在您的视图模型中设置函数

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

In your xml file:在您的 xml 文件中:

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

In your code like this in your model class :在您的模型类中这样的代码中:

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);
    }
}

For more information about it read this Doc.有关它的更多信息,请阅读此文档。 Also include in your answer app:textChangedListener in EditText and try.还包括在您的答案app:textChangedListenerEditText并尝试。

I hope it'll help you...!我希望它会帮助你...!

You can use 2-way binding if you're using data binding like below,如果您使用如下数据绑定,则可以使用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" />

Then in your viewmodel,然后在你的视图模型中,

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. 
}

You need to define a BindingAdapter in order to use it in XML like android:afterTextChanged您需要定义一个BindingAdapter才能在 XML 中使用它,例如android:afterTextChanged

Define somewhere in your project定义项目中的某处

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

The code is taken reference here: this link代码在此处参考: 此链接

暂无
暂无

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

相关问题 Android数据绑定-找不到属性的获取器 - Android Data-binding - Cannot find the getter for attribute Android数据绑定:找不到属性app:list的setter - Android data binding : Cannot find setter for attribute app:list 如何解决:“在使用自定义视图实现双向数据绑定时,找不到属性&#39;android:text&#39;”的getter? - How to solve: “cannot find getter for attribute 'android:text'” when implementing two-way data binding with custom view? 在数据绑定中找不到属性的设置器 - Cannot find the setter for attribute in Data binding Android:绑定到 EditText 时触发 TextWatcher.onTextChanged - Android: TextWatcher.onTextChanged fires when binding to EditText Android - 数据绑定找不到方法,使用 BottomSheetBehavior - Android - Data Binding cannot find method, using BottomSheetBehavior AndroidX迁移-数据绑定错误msg:找不到参数类型为int的属性“ android:visibility”的设置器 - AndroidX migration - data binding error msg:Cannot find the setter for attribute 'android:visibility' with parameter type int msg:在数据绑定中找不到值为java.lang.String的属性&#39;android:text&#39;的getter? - msg:Cannot find the getter for attribute 'android:text' with value type java.lang.String in data binding? Android数据绑定错误:找不到符号 - android data binding error: cannot find symbol Android数据绑定BindingAdapter:“找不到设置器” - Android Data Binding BindingAdapter: “Cannot find the setter”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM