首页 > 解决方案 > 找不到接受参数类型“android.text.TextWatcher”的 android.widget.EditText android:onTextChanged 的​​设置器

问题描述

在 android 中使用数据绑定实现 TextWatcher 时出现问题。如何解决此问题请帮助。

这是我的 MainActivity 类代码。

BottomSheetDialog mBottomSheetDialog = new BottomSheetDialog(SplashActivity.this,R.style.AppBottomSheetDialogTheme);
        mBottomSheetDialog.setCancelable(false);
        mBottomSheetDialog.setCanceledOnTouchOutside(true);
        DialogBottomSheetLoginBinding bottomSheetLoginBinding= DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.dialog_bottom_sheet_login, null, false);
        mBottomSheetDialog.setContentView(bottomSheetLoginBinding.getRoot());
        mBottomSheetDialog.show();

    mViewModel = new EditTextViewModel();
    bottomSheetLoginBinding.setData(mViewModel);

public EditTextViewModel(){
    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(final Editable s) {
            if (s.length() >= 10) {

                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        // TODO: do what you need here (refresh list)
                        // you will probably need to use
                        // runOnUiThread(Runnable action) for some specific
                        // actions
                        Log.d("OntextChange",s.toString());
                    }

                }, DELAY);
            }
        }

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

xml文件:

    <variable
        name="data"
        type="com.smsdaakindia.instantpay.viewmodel.EditTextViewModel" />
</data>

<androidx.core.widget.NestedScrollView
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:orientation="vertical"
            android:padding="10dp">

            <com.an.customfontview.CustomTextView
                android:id="@+id/otp_text"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginTop="30dp"
                android:text="@string/login_enter_phone"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_xxlarge"
                app:textFontPath="fonts/OpenSans-Regular.ttf" />

            <LinearLayout
                android:id="@+id/ll_no"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_below="@id/otp_text"
                android:layout_marginTop="30dp"
                android:background="@drawable/card_background"
                android:gravity="center_vertical"
                android:orientation="horizontal">

                <com.an.customfontview.CustomTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20dp"
                    android:layout_marginLeft="20dp"
                    android:text="+91"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_xxlarge"
                    app:textFontPath="fonts/OpenSans-Regular.ttf" />

                <EditText
                    android:id="@+id/et_mobile"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:inputType="number"
                    android:maxLength="10"
                    android:paddingLeft="5dp"
                    android:textColor="@color/colorPrimary"
                    android:textCursorDrawable="@null"
                    android:textSize="@dimen/textSize_xxlarge"
                    android:textStyle="bold"
                    android:onTextChanged="@{data.textWatcher}"
                    />

            </LinearLayout>

            <com.an.customfontview.CustomTextView
                android:id="@+id/btn_confirm"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_below="@+id/ll_no"
                android:layout_marginTop="30dp"
                android:background="@drawable/round_button_with_grey"
                android:gravity="center"
                android:text="@string/next"
                android:textAllCaps="false"
                android:textColor="#fff"
                android:textSize="@dimen/textSize_xxlarge"
                app:btnFontPath="fonts/Roboto-Light.ttf" />

            <View
                android:layout_width="150dp"
                android:layout_height="5dp"
                android:layout_below="@+id/btn_confirm"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="30dp"
                android:background="@drawable/round_button_with_grey">

            </View>

        </RelativeLayout>

    </LinearLayout>

当我构建项目时,它显示找不到接受参数类型'android.text.TextWatcher'的设置器

如果绑定适配器提供了设置器,请检查适配器是否正确注释以及参数类型是否匹配。

标签: androidmvvm

解决方案


在你的使用EditText

<EditText
    app:addTextChangedListener="..."
   ../>

代替:

<EditText
   android:onTextChanged="@{data.textWatcher}"
   ../>

推荐阅读