简体   繁体   中英

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

Here is a part of my 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:

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

I hope it'll help you...!

You can use 2-way binding if you're using data binding like below,

<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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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