1

我正在使用默认的 Android Gboard 键盘。但是当我inputType="number"EditText. 显示键盘但类型值未显示在 中EditText,也未在 TextWatcher 中获取任何类型值。

EditTextinListView元素如下所示:

<EditText
    android:id="@+id/editTextChecked"
    android:layout_width="150dp"
    android:layout_height="60dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_gravity="center"
    android:gravity="center"      
    android:inputType="number"
    android:textColor="#000000" />

ListView定义为:

<ListView
    android:id="@+id/listViewDialog"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bottom"
    android:layout_below="@+id/editTextSearch"
    android:background="@android:color/white"
    android:divider="@android:color/darker_gray"
    android:dividerHeight="1dp" />

这是我的 TextWather 代码:-

 holder.etCheck.addTextChangedListener(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) {
            Log.e("text",""+s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
4

1 回答 1

0

问题TextWatcher是它不能很好地与ListViews 一起使用。您必须像这样定义自己的自定义TextWatcher

private class MyCustomEditTextListener implements TextWatcher {
    private int position;

    public void updatePosition(int position) {
        this.position = position;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        // do your operations here.
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        Logger.e(TAG, "After" + editable.toString());
    }      
}

并在里面getView()这样做:

((Viewholder) holder).myCustomEditTextListener.updatePosition(position);

最后在你viewHolder初始化你的内部EditText并将自定义添加TextWatcher到它。

于 2017-07-14T07:21:59.047 回答