EditText输入过滤器导致字母重复 [英] EditText input filter causing repeating letters

查看:218
本文介绍了EditText输入过滤器导致字母重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直将输入限制为我的edittext;

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);

现在这在普通的android键盘上可以正常工作,但是问题出在其他键盘上(来自Google Play),它不能禁用自动更正功能,因此我遇到了再次重复字符的问题.有没有人遇到这个/知道如何解决?

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?

推荐答案

编辑-这不太起作用.在某些设备(似乎是大多数三星)上,重复字母的问题又回来了-频率略有降低.

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.

我建议以下内容:

  • 使用android:digits xml属性
  • 在需要时检查编辑文本的内容,而不是在其中键入
  • 您可以使用文本观察器,但是我发现这种方法无效-如果您找到了使用文本观察器的可行解决方案,请告诉我.
  • 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();
    }
}
}

这篇关于EditText输入过滤器导致字母重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆