-
-
Save kaleai/3cadefbc35283a4855aa to your computer and use it in GitHub Desktop.
fdf
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 static void lengthChineseFilter(final EditText editText, final int length) { | |
InputFilter[] filters = new InputFilter[1]; | |
filters[0] = new InputFilter.LengthFilter(length) { | |
public CharSequence filter(@NonNull CharSequence source, int start, int end, @NonNull Spanned dest, int dstart, int dend) { | |
// 可以检查中文字符 | |
boolean isChinese = CharacterUtil.checkNameChese(source.toString()); | |
if (!isChinese || dest.toString().length() >= length) { | |
return ""; | |
} | |
return source; | |
} | |
}; | |
// Sets the list of input filters that will be used if the buffer is Editable. Has no effect otherwise. | |
editText.setFilters(filters); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment