问题
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?
回答1:
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();
}
}
}
回答2:
Use this in the EditText
in your XML to fix this issue:
android:inputType="textFilter"
来源:https://stackoverflow.com/questions/16178059/edittext-input-filter-causing-repeating-letters