public static class NameLengthFilter implements InputFilter {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
int destLen = dest.toString().getBytes("GB18030").length;
int sourceLen = source.toString().getBytes("GB18030").length;
if (destLen + sourceLen > 12) {
return "";
}
if (source.length() < 1 && (dend - dstart >= 1)) {
return dest.subSequence(dstart, dend - 1);
}
return source;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
}
mEditText.setFilters(new InputFilter[] { new NameLengthFilter() });