【问题标题】:Cannot find attribute "android:onTextChanged" in xml when using data binding使用数据绑定时在 xml 中找不到属性“android:onTextChanged”
【发布时间】:2020-04-19 03:36:02
【问题描述】:

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

data-binding { enable = true }

在我的 build.gradle 中,我当前的 build 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" />
...

【问题讨论】:

  • 你用的是kotlin还是java?
  • @haresh 我在我的项目中同时使用 java 和 kotlin。但主要是java。这是我找不到onTextChanged属性的原因吗?
  • 兄弟你找到解决办法了吗??我也来自那个问题..

标签: android xml data-binding android-jetpack


【解决方案1】:

你可以使用

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

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

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

【讨论】:

  • 问题是我在EditText中找不到任何这些属性,包括android:onTextChangedandroid:beforeTextChangedandroid:afterTextChanged
【解决方案2】:

在您的 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);
    }
}

有关它的更多信息,请阅读此Doc. 并在EditText 中包含您的答案app:textChangedListener 并尝试。

希望对你有帮助……!

【讨论】:

  • 感谢您的回答,但这并不能解决我的问题。我在EditText 中找不到属性onTextChanged,即使他们说它是开箱即用的。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

    在你的项目中定义某处

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

    代码参考这里:this link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      相关资源
      最近更新 更多