的EditText和输入过滤原因重复文本 [英] EditText and InputFilter cause repeating text

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

问题描述

我想实现限制输入字母字符只有[A-ZA-Z]一个EditText。

我开始从<一个的输入过滤方法href="http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android">this帖子。当我键入​​A%的文本消失,然后,如果我打退格的文本是一个。我试着像使用正则表达式只匹配[A-ZA-Z],有时看到这样重复的字符疯狂的行为在过滤器功能等的变化,我会键入A,然后B,并获得AAB,那么键入C,并获得aabaabc然后按退格并获得aabaabcaabaabc!

这里的code我的工作迄今与不同的方法我试过。

 的EditText输入=(EditText上)findViewById(R.id.inputText);
    输入过滤器过滤器=新的输入过滤器(){
        @覆盖
        公共CharSequence的过滤器(CharSequence的来源,诠释开始,诠释年底,跨区DEST,INT DSTART,诠释DEND){
            //字符串数据= source.toString();
            //字符串RET = NULL;
            / *
            布尔的isValid = data.matches([A-ZA-Z]);
            如果(参考isValid){
                RET = NULL;
            }
            其他 {
                RET = data.replaceAll([@#$%^&放大器; *],);
            }
            * /
            / *
            DEST =新SpannableStringBuilder();
            RET = data.replaceAll([@#$%^&放大器; *],);
            返回RET;
            * /

            的for(int i =启动; I&LT;结束;我++){
                如果(!Character.isLetter(source.charAt(ⅰ))){
                    返回 ;
                }
            }

            返回null;
        }
    };
    input.setFilters(新输入过滤器[] {滤});
 

我完全难倒这一个所以这里的任何帮助将不胜AP preciated。

编辑: 好吧,我已经做了不少与输入过滤实验,并且已经得出了一些结论,尽管没有解决的问题。请参阅下面我的code的评论。我要去尝试伊姆兰林蛙的解决方案了。

 的EditText输入=(EditText上)findViewById(R.id.inputText);
    输入过滤器过滤器=新的输入过滤器(){
        //目前尚不清楚这是什么函数返回!
        //文档说返回空值,以使新的字符(S),并返回来禁止
        //但在返回时的行为是不一致的。
        //
        //源参数是SpannableStringBuilder如果1字符输入,但它
        //等于从EditText上整个字符串。
        //如果有多个字符输入(如与某些键盘该自动插入件的情况下
        //某些字符后面输入一个空格),那么源参数是一个CharSequence中,只有等于
        //新的字符。
        @覆盖
        公共CharSequence的过滤器(CharSequence的来源,诠释开始,诠释年底,跨区DEST,INT DSTART,诠释DEND){
            字符串数据= source.toString()子(开始,结束)。
            字符串retData = NULL;

            布尔的isValid = data.matches([A-ZA-Z] +);
            如果(!的isValid){
                如果(源的instanceof SpannableStringBuilder){
                    //这工作,直到下一个字符进行评估,那么你得到重复
                    //(输入A,那么^给出是,然后输入B赋予AAB)
                    retData = data.replaceAll([@#$%^&放大器; *],);
                    //如果我不是一直在这里返回一个空字符串那么的EditText是空白。
                    //(输入A,那么^给)
                    // retData =;
                }
                其他{//源的instanceof的CharSequence
                    //我们只拿到这里如果输入超过1个字符(如&放大器;)。
                    //再次,这工作,直到下一个字符进行评估,那么你得到重复
                    //(输入A,然后&放大器;给出了A,然后进入B赋予AAB。)
                    retData =;
                }
            }

            返回retData;
        }
    };
    input.setFilters(新输入过滤器[] {滤});
 

解决方案

宾果,我发现这个问题!

当我使用的android:cursorVisible =假的EditText上开始和DSTART参数不正确匹配

start参数仍始终为0对我来说,但DSTART参数也总是0,因此它的作品了,只要我使用.replaceAll()。这是相反的是<一个href="http://stackoverflow.com/questions/9815433/can-someone-help-me-with-the-parameters-to-the-android-inputfilter-filter-meth">this帖子是这么说的,我不明白为什么,但至少我可以建立一些作品吧!

I'm trying to implement an EditText that limits input to alpha chars only [A-Za-z].

I started with the InputFilter method from this post. When I type "a%" the text disappears then if I hit backspace the text is "a". I've tried other variations on the filter function like using a regex to match only [A-Za-z] and sometimes see crazy behavior like repeating chars, I'll type "a" then "b" and get "aab" then type "c" and get "aabaabc" then hit backspace and get "aabaabcaabaabc"!

Here's the code I'm working with so far with the different approaches I've tried.

    EditText input = (EditText)findViewById( R.id.inputText );
    InputFilter filter = new InputFilter() {
        @Override
        public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {
            //String data = source.toString();
            //String ret = null;
            /*
            boolean isValid = data.matches( "[A-Za-z]" );
            if( isValid ) {
                ret = null;
            }
            else {
                ret = data.replaceAll( "[@#$%^&*]", "" );
            }
            */
            /*
            dest = new SpannableStringBuilder();
            ret = data.replaceAll( "[@#$%^&*]", "" );
            return ret;
            */

            for( int i = start; i < end; i++ ) {
                if( !Character.isLetter( source.charAt( i ) ) ) {
                    return "";
                }
            }

            return null;
        }
    };
    input.setFilters( new InputFilter[]{ filter } );

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

EDIT: Ok, I've done quite a lot of experimenting with InputFilter and have drawn some conclusions, albeit no solution to the problem. See the comments in my code below. I'm going to try Imran Rana's solution now.

    EditText input = (EditText)findViewById( R.id.inputText );
    InputFilter filter = new InputFilter() {
        // It is not clear what this function should return!
        // Docs say return null to allow the new char(s) and return "" to disallow
        // but the behavior when returning "" is inconsistent.
        // 
        // The source parameter is a SpannableStringBuilder if 1 char is entered but it 
        // equals the whole string from the EditText.
        // If more than one char is entered (as is the case with some keyboards that auto insert 
        // a space after certain chars) then the source param is a CharSequence and equals only 
        // the new chars.
        @Override
        public CharSequence filter( CharSequence source, int start, int end, Spanned dest, int dstart, int dend ) {
            String data = source.toString().substring( start, end );
            String retData = null;

            boolean isValid = data.matches( "[A-Za-z]+" );
            if( !isValid ) {
                if( source instanceof SpannableStringBuilder ) {
                    // This works until the next char is evaluated then you get repeats 
                    // (Enter "a" then "^" gives "a". Then enter "b" gives "aab")
                    retData = data.replaceAll( "[@#$%^&*']", "" );
                    // If I instead always returns an empty string here then the EditText is blanked.
                    // (Enter "a" then "^" gives "")
                    //retData = "";
                }
                else { // source is instanceof CharSequence
                    // We only get here if more than 1 char was entered (like "& ").
                    // And again, this works until the next char is evaluated then you get repeats 
                    // (Enter "a" then "& " gives "a". Then enter "b" gives "aab")
                    retData = "";
                }
            }

            return retData;
        }
    };
    input.setFilters( new InputFilter[]{ filter } );

解决方案

Bingo, I found the problem!

When I use android:cursorVisible="false" on the EditText the start and dstart parameters don't match up correctly.

The start parameter is still always 0 for me, but the dstart parameter is also always 0 so it works out as long as I use .replaceAll(). This is contrary to what this post says so I don't quite understand why but at least I can build something that works now!

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

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