Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText input filter causing repeating letters

I have been limiting the input to my edittext like this;

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            String output = "";
            for (int i = start; i < end; i++) {
                if (source.charAt(i)!='~'&&source.charAt(i)!='/') {
                    output += source.charAt(i); 
                }
            } 
            return output;
        }
    };

But anyone who has used this method will know that it causes repeating characters when it is mixed with auto correct and the backspace key. To solve this I removed the auto correct bar from the keyboard like this;

Edittect.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

Now this works fine on the stock android keyboard, but the problem is on alternative keyboards(from Google play) it does not disable the auto correct, and so therefore I run into the problem of repeating characters again. Has anyone encountered this/know how to solve it?

like image 479
jcw Avatar asked Apr 23 '13 19:04

jcw


2 Answers

EDIT - This doesn't quite work. On some devices(it seems like most samsungs) the repeating letter problem comes back - just slightly less often.

I would seriously recommend finding a different way to limit inputs, because the input filter has some serious problems as of the moment.

I suggest the following:

  • use the android:digits xml attribute
  • check the edittext's contents when you need to, not as it is typed in
  • you could play around with the text watcher, but I have found that ineffective - if you find a working solution using a text watcher please let me know.

I figured the problem out - this is what I used in the end

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

    if (source instanceof SpannableStringBuilder) {
        SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder)source;
        for (int i = end - 1; i >= start; i--) { 
            char currentChar = source.charAt(i);
             if (currentChar=='/' || currentChar=='~') {    
                 sourceAsSpannableBuilder.delete(i, i+1);
             }     
        }
        return source;
    } else {
        StringBuilder filteredStringBuilder = new StringBuilder();
        for (int i = 0; i < end; i++) { 
            char currentChar = source.charAt(i);
            if (currentChar != '~'|| currentChar != '/') {    
                filteredStringBuilder.append(currentChar);
            }     
        }
        return filteredStringBuilder.toString();
    }
}
}
like image 60
jcw Avatar answered Nov 10 '22 11:11

jcw


Use this in the EditText in your XML to fix this issue:

android:inputType="textFilter"
like image 38
linguokun Avatar answered Nov 10 '22 10:11

linguokun