Created
September 22, 2016 08:22
-
-
Save deskid/df3dc6393b8177b3d181d28b7db0faa9 to your computer and use it in GitHub Desktop.
EditText.setFilters always over write the origin filters,buggly...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void setMaxLengthFilter(EditText editText, int maxLength) { | |
InputFilter curFilters[]; | |
InputFilter.LengthFilter lengthFilter; | |
int i; | |
lengthFilter = new InputFilter.LengthFilter(maxLength); | |
curFilters = editText.getFilters(); | |
if (curFilters != null) { | |
for (i = 0; i < curFilters.length; i++) { | |
if (curFilters[i] instanceof InputFilter.LengthFilter) { | |
curFilters[i] = lengthFilter; | |
return; | |
} | |
} | |
InputFilter newFilters[] = new InputFilter[curFilters.length + 1]; | |
System.arraycopy(curFilters, 0, newFilters, 0, curFilters.length); | |
newFilters[curFilters.length] = lengthFilter; | |
editText.setFilters(newFilters); | |
} else { | |
editText.setFilters(new InputFilter[]{lengthFilter}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment