Skip to content

Instantly share code, notes, and snippets.

@deskid
Created September 22, 2016 08:22
Show Gist options
  • Save deskid/df3dc6393b8177b3d181d28b7db0faa9 to your computer and use it in GitHub Desktop.
Save deskid/df3dc6393b8177b3d181d28b7db0faa9 to your computer and use it in GitHub Desktop.
EditText.setFilters always over write the origin filters,buggly...
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