5

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?

4

2 回答 2

4

编辑 - 这不太管用。在某些设备上(似乎大多数三星),重复字母问题又回来了——只是稍微少了一点。

我强烈建议找到一种不同的方法来限制输入,因为输入过滤器目前存在一些严重的问题。

我建议如下:

  • 使用android:digitsxml 属性
  • 需要时检查edittext的内容,而不是输入时
  • 您可以使用文本观察器,但我发现这无效 - 如果您找到使用文本观察器的有效解决方案,请告诉我。

我发现了问题 - 这就是我最后使用的

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();
    }
}
}
于 2013-04-24T06:08:55.557 回答
1

EditText在您的XML中使用它来解决此问题:

android:inputType="textFilter"
于 2018-05-10T06:19:38.140 回答