Note that there are some explanatory texts on larger screens.

plurals
  1. POEditText and InputFilter cause repeating text
    text
    copied!<p>I'm trying to implement an EditText that limits input to alpha chars only [A-Za-z]. </p> <p>I started with the InputFilter method from <a href="https://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android">this post</a>. 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"!</p> <p>Here's the code I'm working with so far with the different approaches I've tried.</p> <pre><code> 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( "[@#$%^&amp;*]", "" ); } */ /* dest = new SpannableStringBuilder(); ret = data.replaceAll( "[@#$%^&amp;*]", "" ); return ret; */ for( int i = start; i &lt; end; i++ ) { if( !Character.isLetter( source.charAt( i ) ) ) { return ""; } } return null; } }; input.setFilters( new InputFilter[]{ filter } ); </code></pre> <p>I'm totally stumped on this one so any help here would be greatly appreciated.</p> <p><strong>EDIT:</strong> 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.</p> <pre><code> 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( "[@#$%^&amp;*']", "" ); // 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 "&amp; "). // And again, this works until the next char is evaluated then you get repeats // (Enter "a" then "&amp; " gives "a". Then enter "b" gives "aab") retData = ""; } } return retData; } }; input.setFilters( new InputFilter[]{ filter } ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload