InputFilter主要是對輸入的文本進(jìn)行過濾的帜讲,里面只有一個filter方法
//InputFilter接口开皿,需要重寫filter方法
public interface InputFilter
{
/**
* @param source 輸入的文字
* @param start 輸入-0懒浮,刪除-0
* @param end 輸入-source文字的長度奔坟,刪除-0
* @param dest 原先顯示的內(nèi)容
* @param dstart 輸入-原光標(biāo)位置叨恨,刪除-光標(biāo)刪除結(jié)束位置
* @param dend 輸入-原光標(biāo)位置柳刮,刪除-光標(biāo)刪除開始位置
* @return
*/
//主要重寫這個方法
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend);
/**
* This filter will capitalize all the lower case letters that are added
* through edits.
*/
//接口內(nèi)的靜態(tài)內(nèi)部類,痒钝,秉颗,輸入小寫轉(zhuǎn)換成大寫
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
}
}
/**
* This filter will constrain edits not to make the length of the text
* greater than the specified length.
*/
// 長度過濾器,限制輸入的長度
public static class LengthFilter implements InputFilter {
private final int mMax;
public LengthFilter(int max) {
mMax = max;
}
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);
}
}
/**
* @return the maximum length enforced by this input filter
*/
public int getMax() {
return mMax;
}
}
}
使用:
editext.setText(string);
editext.setFilters(filter);
Editext的setText函數(shù):
private void setText(CharSequence text, BufferType type,
boolean notifyBefore, int oldlen) {
......//其他代碼
int n = mFilters.length;
//重點(diǎn):設(shè)置文本需要提前經(jīng)過設(shè)置的所有過濾器filter
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;
}
}
if (notifyBefore) {
if (mText != null) {
oldlen = mText.length();
sendBeforeTextChanged(mText, 0, oldlen, text.length());
} else {
sendBeforeTextChanged("", 0, 0, text.length());
}
}
......//其他代碼
//監(jiān)聽器TextWatcher的接口
sendOnTextChanged(text, 0, oldlen, textLength);
onTextChanged(text, 0, oldlen, textLength);
notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);
if (needEditableForNotification) {
sendAfterTextChanged((Editable) text);
} else {
// Always notify AutoFillManager - it will return right away if autofill is disabled.
notifyAutoFillManagerAfterTextChangedIfNeeded();
}
// SelectionModifierCursorController depends on textCanBeSelected, which depends on text
if (mEditor != null) mEditor.prepareCursorControllers();
}
1.不讓輸入框輸入內(nèi)容
private InputFilter[] filter = new InputFilter[] {
new InputFilter() {
// 不讓輸入框輸入內(nèi)容
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return null;
}
},
/**這里限制輸入的長度為5個字母*/
new InputFilter.LengthFilter(5),
/**輸入小寫轉(zhuǎn)換成大寫*/
new InputFilter.AllCaps();
};
2.只要你輸入內(nèi)容都會替換成“I LOVE YOU”送矩,刪除 - 正常刪除
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (end > 0){
return "I LOVE YOU";
}else {
return null;
}
}
3. 控制不讓輸入空格蚕甥,不讓輸入數(shù)字大于13位(解決手機(jī)號輸入問題)
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if(source.equals(" ") || source.toString().contentEquals("\n") || dstart == 13)return "";
else return null;
}
4.不讓輸入框接著輸入內(nèi)容(原來有內(nèi)容,禁止輸入)
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
return dest.subSequence(dstart,dend);