4

我试过TextWatcher通过afterTextChanged. EditText如果用户在达到限制后尝试插入字符,我想要的只是显示一条 toast 消息或设置错误。我认为这是可能的。但不知道如何实现这一点。

更新

我的 EditText 编码是

<EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:maxLength="10"
        android:layout_marginTop="150dp"
        android:inputType="numberDecimal" >

在这里,如果用户尝试输入超过 10 位数字,我想显示祝酒词或设置错误。

更新

 editText1.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (editText1.getText().length() >= 10)
                Toast.makeText(getApplicationContext(), "hi",
                        Toast.LENGTH_SHORT).show();
            return false;
        }
    });
4

5 回答 5

1

On key listener 在 Soft Input Keyboard 上不起作用。来自 Android 参考:

http://developer.android.com/reference/android/view/View.OnKeyListener.html

View.OnKeyListener

类概述 将硬件按键事件分派到此视图时要调用的回调的接口定义。将在将键事件提供给视图之前调用回调。这仅对硬件键盘有用;软件输入法没有义务触发这个监听器。

所以这意味着 TextWatcher 只是你的编辑框和键盘输入的朋友。

于 2013-11-06T10:00:13.280 回答
0

你可以editTextthis得到你的 maxLength 。然后实现一个keyListener。

 int maxLength = //getMaxLenght here;
    editText.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(editText.getText().toString().length()>= maxLength) {
                //Show toast here
            }
            return false;
        }
    });
于 2013-06-10T16:46:20.790 回答
0

假设您知道 EditText 的最大长度,您可以EditText.setError(String errorMsg)像这样使用:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (charSequence.toString().length() >= maxLength) {
            // show toast / error here
            String limitReached = "Maximum limit of characters reached!";
            editText.setError(limitReached);
        } else {
            // clear error
            editText.setError(null);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {

    }
});

它看起来像这样。当该 EditText 不在焦点上时,不会显示错误消息,但错误图标仍然可见。您可以EditText.getError()用来检查当前是否在 EditText 上设置了错误。

EditText.setError() 外观示例

这可能比您要求的要多,但是如果您有一个具有动态字符限制的 EditText,我建议您在maxLength变量中的某个位置保留该值。(例如,支付卡的安全码因卡类型而异)

但是,您可以执行以下操作以编程方式检查android:maxLength当前设置的 XML 值:

    private int getEditTextCharacterLimit(EditText editText) {
        int defaultMaxLength = 5; // default value

        InputFilter[] filters = editText.getFilters();
        if (filters != null && filters[0] != null && filters[0] instanceof InputFilter.LengthFilter) {
            return ((InputFilter.LengthFilter) filters[0]).getMax();
        } else {
            return defaultMaxLength;
        }
    }

以下是您如何以编程方式动态更改 EditText 的字符限制。

于 2019-04-04T03:43:20.313 回答
0

这可能比您要求的要多,但是如果您有一个具有动态字符限制的 EditText,我建议您在 maxLength 变量的某处保留该值。(例如,支付卡的安全码因卡类型而异)

于 2019-04-30T21:29:41.630 回答
0
 val maxLength = 20
        edtPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            }
            override fun afterTextChanged(s: Editable) {
                if (s.length > maxLength) {
                    s.delete(maxLength, s.length)
                    errorPassword.value = getLocalizationString(
                        R.string.passwordMaxLengthErrorMessage,
                        viewModel.maxPasswordLength
                    )
                }
            }
        })

我正在处理同样的问题,这在我的代码中运行良好(Kotlin)

于 2022-01-13T06:43:23.210 回答