Skip to content

Instantly share code, notes, and snippets.

@kaleai
Created November 2, 2015 10:40
Show Gist options
  • Save kaleai/283b7162a5437d992e3c to your computer and use it in GitHub Desktop.
Save kaleai/283b7162a5437d992e3c to your computer and use it in GitHub Desktop.
editText
/**
* 限制中文字符的长度
*/
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