如何在 Android xml 中正确添加 TextChangeListener

How to correctly addTextChangeListener in Android xml

我已尝试执行与此处相同的操作: https://gist.github.com/susemi99/a45ca534cc109271f34e6c992f69f048

所以我在 xml 中有我的 EditText:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <import type="test.edu.MyViewModel"/>
    <variable type="MyViewModel" name="viewModel"/>
</data>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

(...)

<EditText 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:addTextChangeListener="@{viewModel.myTextWatcher}"/>

(...)

我在 MyViewModel 中有 TextWatcher:

public class MyViewModel extends BaseObservable {
    public TextWatcher myTextWatcher = new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void afterTextChanged(Editable editable) {}
    };
}

我认为我已经完成了与提到的相同 link 但是当我尝试编译时我得到:

Cannot find the setter for attribute 'app:addTextChangeListener' with parameter type android.text.TextWatcher on android.widget.EditText.

你能给我一些关于这里有什么问题的提示吗?我假设我引用的代码正在编译,但我不是 100% 确定。

你可以这样试试:

    EditText editText =(EditText)findViewById(R.id.editText1);
    editText.addTextChangeListener(new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}

        @Override
        public void afterTextChanged(Editable editable) {}
    };

而不是将其写入您的 xml 文件

我终于想通了...这只是一个错字。而不是 addTextChangedListener 我有 addTextChangeListener 老实说,错误消息不是很有帮助。现在,当我知道解决方案时,它是显而易见的,但之前我正在检查 "app" 命名空间、一些 @Bind 注释、gradle 选项和类似的东西。也许只有我一个人,但我认为这样的错误会容易得多:

android.widget.EditText doesn't have function addTextChangeListener