引言

在开发中难免会碰到限制EditText长的的需求,最简单的做法就是设置maxLength属性,但是这个是不准确的,他的计算方式是按照字符数,算的,但是现实使用中,同样字符长度的汉字是比英文或者字符要长的,所以要按照是否中文和英文或者混排,来限制长度,一般汉字按照两个来计算的。Android里边使用自定义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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class CustomCoinNameFilter implements InputFilter {

private int maxLength;//最大长度,ASCII码算一个,其它算两个

public CustomCoinNameFilter(int maxLength) {
this.maxLength = maxLength;
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (TextUtils.isEmpty(source)) {
return null;
}
LogUtils.e("source:" + source);
LogUtils.e("dest:" + dest);
int inputCount = 0;
int destCount = 0;
inputCount = getCurLength(source);
LogUtils.e("inputCount:" + inputCount);
if (dest.length() != 0)
destCount = getCurLength(dest);
LogUtils.e("destCount:" + destCount);
if (destCount >= maxLength)
return "";
else {

int count = inputCount + destCount;
if (dest.length() == 0) {
if (count <= maxLength)
return null;
else
return sub(source, maxLength);
}
LogUtils.e("count:" + count);
if (count > maxLength) {
//int min = count - maxLength;
int maxSubLength = maxLength - destCount;
return sub(source, maxSubLength);
}
}
return null;
}

private CharSequence sub(CharSequence sq, int subLength) {
int needLength = 0;
int length = 0;
for (int i = 0; i < sq.length(); i++) {
if (sq.charAt(i) < 128)
length += 1;
else
length += 2;
++needLength;
if (subLength <= length) {
return sq.subSequence(0, needLength);
}
}
return sq;
}

private int getCurLength(CharSequence s) {
int length = 0;
if (s == null)
return length;
else {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) < 128)
length += 1;
else
length += 2;
}
}
return length;
}
}

使用

1
mSetWalletName.setFilters(new InputFilter[]{new CustomCoinNameFilter(20)});