使用EditText的过程中,有时会遇到disable(禁止输入)的需求,直接使用setEnabled(false)是无效的。网上搜索发现可以通过设置InputFilter来实现
[java]
myEditText.setInputFilters(new InputFilter[]{
new InputFilter() {
@Override
filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return dest.subSequence(dstart, dend);
}
}
});
那么到底是如何实现disable的呢?先来看看InputFilter接口实现类的介绍
InputFilter.AllCaps
--This filter will capitalize all the lower case letters that are added through edits
InputFilter.LengthFillter
--This filter will constrain edits not to make the length of the text greater than the specified length
NumberKeyListener
--For numeric text entry
DateKeyListener
--For entering dates in a text field
DateTimeKeyListener
--For entering dates and times in the same text field
DialerKeyListener
--For dialing-only text entry
DigitsKeyListener
--For digits-only text entry
TimeKeyListener
--For entering times in a text field
LoginFilter
--Abstract class for filtering login-related text (user names and passwords)
LoginFilter.PasswordFilterGMail
--This filter is compatible with GMail passwords which restricts characters to the Latin-1 (ISO8859-1) char set
LoginFilter.UsernameFilterGMail
--This filter rejects characters in the user name that are not compatible with GMail account creation
LoginFilter.UsernameFilterGeneric
--This filter rejects characters in the user name that are not compatible with Google login
可以看出,InputFilter主要是对用户的(输入)文本进行过滤的,这个接口只有一个filter方法
先看看InputFilter.AllCaps的实现原理
[java]
public static class AllCaps implements InputFilter {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isLowerCase(source.charAt(i))) {
char[] v = new char[end - start];
TextUtils.getChars(source, start, end, v, 0);
String s = new String(v).toUpperCase();
if (source instanceof Spanned) {
SpannableString sp = new SpannableString(s);
TextUtils.copySpansFrom((Spanned) source,
start, end, null, sp, 0);
return sp;
} else {
return s;
}
}
}
return null; // keep original
}
}
从代码不难发现,当字符串source中包含小写字母时,将其转换为大写字母然后返回,从而实现AllCaps。
再来看看InputFilter.LengthFillter的实现原理
[java]
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
int keep = mMax - (dest.length() - (dend - dstart));
if (keep <= 0) {
return "";
} else if (keep >= end - start) {
return null; // keep original
} else {
keep += start;
if (Character.isHighSurrogate(source.charAt(keep - 1))) {
--keep;
if (keep == start) {
return "";
}
}
return source.subSequence(start, keep);
}
}
其中mMax是文本的长度上限,keep是mMax与当前text的长度的差值。
当keep小于0时,也就是文本已经超过长度上限时,返回""
当文本没超过长度上限时,返回null,保持文本原样不变(keep original)
最后一个else暂时可以不考虑
再来看看EditText是如何使用这个InputFilter的,其实InputFilter是在TextView中用到的,在TextView的setText()方法中
[java]
int n = mFilters.length;
for (int i = 0; i < n; i++) {
CharSequence out = mFilters[i].filter(text, 0, text.length(), EMPTY_SPANNED, 0, 0);
if (out != null) {
text = out;
}
}
sendOnTextChanged(text, 0, oldlen, textLength);
onTextChanged(text, 0, oldlen, textLength);
遍历所有的InputFilter,如果filter后的结果不是null,更新text变量。这意味着filter返回null就代表保持原文本不变(如上,在InputFilter.LengthFilter中看到的)。所有InputFilter后的结果就是最终显示在TextView中的文本。
了解了InputFilter的原理,就知道如何对TextView/EditText的文本做“手脚”了。比如
不响应所有输入的特殊字符(@#$%^&)
显示最多只能输入100个大写字母(使用InputFilter.LengthFilter和InputFilter.Allcaps的组合)
将输入的某个字符自动替换为其他字符
回头再看看disable EditText的那个InputFilter,对EditText来说,调用filter()的后三个参数始终是EMPTY_SPANNED, 0, 0,当执行filter时,就相当于执行EMPTY_SPANNED.subSequence(0, 0),永远返回"",从而实现了disable。