InputFilter on EditText cause repeating text

2019-04-04 07:07发布

I'm trying to implement an EditText that limits input to Capital chars only [A-Z0-9] with digits as well.

I started with the InputFilter method from some post.But here I am getting one problem on Samsung Galaxy Tab 2 but not in emulator or Nexus 4.

Problem is like this :

  1. When I type "A" the text shows as "A" its good
  2. Now when I type "B" so text should be "AB" but it gives me "AAB" this looks very Strange.

In short it repeats chars

Here's the code I'm working with this code :

public class DemoFilter implements InputFilter {

    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
            int dend) {

        if (source.equals("")) { // for backspace
            return source;
        }
        if (source.toString().matches("[a-zA-Z0-9 ]*")) // put your constraints
                                                        // here
        {
            return source.toString().toUpperCase();
        }
        return "";
    }
}

XML file code :

<EditText
    android:id="@+id/et_licence_plate_1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:hint="0"
    android:imeOptions="actionNext"
    android:inputType="textNoSuggestions"
    android:maxLength="3"
    android:singleLine="true"
    android:textSize="18px" >
</EditText>

I'm totally stuck up on this one, so any help here would be greatly appreciated.

9条回答
聊天终结者
2楼-- · 2019-04-04 07:55

I've met this problem few times before. Setting some kinds of inputTypes in xml propably is the source of problem. To resolve it without any additional logic in InputFilter or TextWatcher just set input type in code instead xml like this:

editText.setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

查看更多
何必那么认真
3楼-- · 2019-04-04 08:06

Same for me, InputFilter duplicates characters. This is what I've used:

Kotlin version:

private fun replaceInvalidCharacters(value: String) = value.replace("[a-zA-Z0-9 ]*".toRegex(), "")

textView.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

    override fun afterTextChanged(s: Editable) {}

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        val newValue = replaceInvalidCharacters(s.toString())
        if (newValue != s.toString()) {
            textView.setText(newValue)
            textView.setSelection(textView.text.length)
        }
    }
})

works well.

查看更多
倾城 Initia
4楼-- · 2019-04-04 08:08

InputFilters can be attached to Editable S to constrain the changes that can be made to them. Refer that it emphasises on changes made rather than whole text it contains..

Follow as mentioned below...

 public class DemoFilter implements InputFilter {

        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart,
                int dend) {

            if (source.equals("")) { // for backspace
                return source;
            }
            if (source.toString().matches("[a-zA-Z0-9 ]*")) // put your constraints
                                                            // here
            {
               char[] ch = new char[end - start];

              TextUtils.getChars(source, start, end, ch, 0);

                // make the characters uppercase
                String retChar = new String(ch).toUpperCase();
                return retChar;
            }
            return "";
        }
    }
查看更多
登录 后发表回答