/**
* InputFilters can be attached to {@link Editable}s to constrain the
* changes that can be made to them.
*/
public interface InputFilter
{
/**
* This method is called when the buffer is going to replace the
* range <code>dstart … dend</code> of <code>dest</code>
* with the new text from the range <code>start … end</code>
* of <code>source</code>. Return the CharSequence that you would
* like to have placed there instead, including an empty string
* if appropriate, or <code>null</code> to accept the original
* replacement. Be careful to not to reject 0-length replacements,
* as this is what happens when you delete text. Also beware that
* you should not attempt to make any changes to <code>dest</code>
* from this method; you may only examine it for context.
*
* Note: If <var>source</var> is an instance of {@link Spanned} or
* {@link Spannable}, the span objects in the <var>source</var> should be
* copied into the filtered result (i.e. the non-null return value).
* {@link TextUtils#copySpansFrom} can be used for convenience.
*/
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend);
}
參數說明:
source:即将輸入的文本
start:在輸入的時候是0,删除的時候也是0
end:在輸入的時候是source.length,在删除的時候是0
dest:在輸入動作發生之前的文本
dstart:輸入時,動作發生前的光标位置;删除時,光标删除的結束位置
dend:輸入時,動作發生前的光标位置;删除時,光标删除的起始位置
return:是你希望添加的内容,如果傳回""則不會添加任何内容,如果傳回null就會将soure全部添加
最後需要注意的是在删除動作觸發時,source的長度是0,這不是一個錯誤哦。另外不能直接修改dest的内容,否則會有神奇的事情發生。