如何防止 EditText 在 Kotlin for Android 中连续 4 次接受字符?

问题描述 投票:0回答:3

如何使编辑文本不接受相同字符 4 次连续。例如,它可以接受“888”而不是“8888”。如果用户写同一个字符 4 次,那么 edittext 应该停止输入

我不确定如何用 textwatcher 实现这个

android android-edittext android-textwatcher
3个回答
1
投票

我认为这是使用 InputFilter 而不是 TextWatcher 最清楚地实现的。如果您拒绝更改,则不必担心将光标放在正确的位置。

弄清楚更改后文本的外观以及是否可以接受。如果不是,请拒绝。

fun CharSequence.hasConsecutiveChars(count: Int) {
    var cur: Char? = null
    var curCount = 0
    for (c in this) {
        if (c != cur) {
            cur = c
            curCount = 0
        }
        if (++curCount >= count) return true
    }
    return false
}

val filter = InputFilter { source, start, end, dest, dstart, dend ->
    val afterChange = source.replaceRange(start..end, dest.subSequence(dstart..dend))
    if (afterChange.hasConsecutiveChars(4)) "" else null
}
editText.setInputFilters(filter)

0
投票

欢迎来到平台@Umar Afzal

试试这个

EditText editText = findViewById(R.id.editText);

editText.setFilters(new InputFilter[] {new InputFilter.LengthFilter(20)});

editText.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) {
        String text = s.toString();
        if (text.length() >= 4 && text.charAt(text.length() - 1) == text.charAt(text.length() - 2)
                && text.charAt(text.length() - 2) == text.charAt(text.length() - 3)
                && text.charAt(text.length() - 3) == text.charAt(text.length() - 4)) {
            editText.setText(text.substring(0, text.length() - 1));
            editText.setSelection(editText.getText().length());
        }
    }

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

0
投票

尝试使用此代码段在用户键入时以及将文本粘贴到

edittext
中时进行处理:

val MAX_CONSEQUENT_CHARS = 4
var internalStopFlag = false
input.doAfterTextChanged { editable ->
        if (internalStopFlag) return@doAfterTextChanged
        editable ?: kotlin.run {
            internalStopFlag = false
            return@doAfterTextChanged
        }

        if (editable.length < MAX_CONSEQUENT_CHARS) kotlin.run {
            internalStopFlag = false
            return@doAfterTextChanged
        }

        internalStopFlag = true

        var consequences = 1
        for (i in 1 until editable.length) {
            if (editable[i] == editable[i - 1]) {
                consequences++
            } else {
                consequences = 1
            }

            if (consequences >= MAX_CONSEQUENT_CHARS) {
                // Before making any changes to Editable be sure to reset the flag. Because
                // tht doAfterTextChange callback will immediately call with the new value of
                // the Editable

                internalStopFlag = false

                editable.delete(i, i + 1)

                // Every time you change the editable you need to break the loop,
                // because the callback will call again immediately
                break
          }
      }

      internalStopFlag = false
}
© www.soinside.com 2019 - 2024. All rights reserved.