Limit input for EditText with InputFilter

项目中经常遇到EditText需要限制输入的情况,虽然EditText本身inputType自带了很多类型,如果遇到需要自定义输入限制时可以使用InputFilter接口,

例如需要限制输入表情符号,可以这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

public static class EmojiFilter implements InputFilter {

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
int dstart, int dend) {
if (source.length() >= 2) {
if (containsEmoji(source.toString())) {
return "";
}
}
return null;
}
}

/**
* 检测是否有emoji表情
*
* @param source
* @return
*/
public static boolean containsEmoji(String source) {
int len = source.length();
for (int i = 0; i < len; i++) {
char codePoint = source.charAt(i);
if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
return true;
}
}
return false;
}

/**
* 判断是否是Emoji
*
* @param codePoint 比较的单个字符
* @return
*/
private static boolean isEmojiCharacter(char codePoint) {
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000));
}

protected void addInputFilter(InputFilter filter) {
if (filter == null) {
throw new IllegalArgumentException();
}
InputFilter[] filters = getFilters();
InputFilter[] detFilters = new InputFilter[(filters == null ? 0 : filters.length) + 1];
if (filters != null) {
System.arraycopy(filters, 0, detFilters, 0, filters.length);
}
detFilters[detFilters.length - 1] = filter;
setFilters(detFilters);
}

然后使用addInputFilter添加到EditText中即可。

0%