簡體   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