EditText和InputFilter导致重复文本

Roo*_*242 13 android android-edittext

我正在尝试实现一个EditText,它只将输入限制为alpha字符[A-Za-z].

我从这篇文章开始使用InputFilter方法.当我输入"a%"时,文本消失,如果我按退格键,则文本为"a".我已经尝试了过滤器功能的其他变体,比如使用正则表达式只匹配[A-Za-z],有时会看到像重复字符这样的疯狂行为,我会输入"a"然后"b"并获得"aab"然后键入"c"并获取"aabaabc"然后点击退格并获得"aabaabcaabaabc"!

这是我到目前为止使用的代码,我尝试过不同的方法.

    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 } );
Run Code Online (Sandbox Code Playgroud)

我对这一点感到非常难过,所以任何帮助都会非常感激.

编辑: 好的,我已经做了大量的InputFilter实验并得出了一些结论,尽管没有解决问题的办法.请参阅下面的代码中的注释.我现在要尝试Imran Rana的解决方案.

    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 } );
Run Code Online (Sandbox Code Playgroud)

Imr*_*ana 5

使用以下代码:

EditText input = (EditText) findViewById(R.id.inputText);
   input.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
         for( int i = start;i<s.toString().length(); i++ ) {
             if( !Character.isLetter(s.charAt( i ) ) ) {
                input.setText("");
             }
         }

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
   });
Run Code Online (Sandbox Code Playgroud)

如果您希望有效文本保留在 EditText 中:


 input.addTextChangedListener(new TextWatcher() {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
         for( int i = 0;i<s.toString().length(); i++ ) {
             if( !Character.isLetter(s.charAt( i ) ) ) {                    
                s.replace(i, i+1,"");               
             }
         }
    }
   });
Run Code Online (Sandbox Code Playgroud)


Roo*_*242 1

宾果,我发现问题了!

当我在 EditText 上使用 android:cursorVisible="false" 时,start 和 dstart 参数不正确匹配。

对我来说,start 参数仍然始终为 0,但 dstart 参数也始终为 0,因此只要我使用 .replaceAll() 就可以了。这与这篇文章所说的相反,所以我不太明白为什么,但至少我可以构建一些现在有效的东西!