简述:Android中EditText等控件监听文本内容的更改使用的是TextWatcher接口,但是官方文档对该接口中方法的参数说明很模糊,在此记录下对方法参数的认识(随认识的增加逐步完善)
public interface TextWatcher extends NoCopySpan {
/**
* This method is called to notify you that, within <code>s</code>,
* the <code>count</code> characters beginning at <code>start</code>
* are about to be replaced by new text with length <code>after</code>.
* It is an error to attempt to make changes to <code>s</code> from
* this callback.
*/
public void beforeTextChanged(CharSequence s, int start,
int count, int after);
/**
* 文本更改后的回调(使用时发现,小米手机,粘贴时前后都会默认加一个空格,
* 造成参数的数字不准确,不知其他手机有无此情况,此处以键盘输入或删除为
* 准。)
* @param s
* 更改以后现存的字符串
* @param start
* 新增字符:该值为新增字符所在的下标
* 删除字符:该值为删掉字符所在的先
* @param before
* 新增字符:该值为0
* 删除字符:该值为删掉的字符数
* @param count
* 新增字符:值为新增字符数
* 删除字符:值为0
*/
public void onTextChanged(CharSequence s, int start, int before, int count);
/**
* This method is called to notify you that, somewhere within
* <code>s</code>, the text has been changed.
* It is legitimate to make further changes to <code>s</code> from
* this callback, but be careful not to get yourself into an infinite
* loop, because any changes you make will cause this method to be
* called again recursively.
* (You are not told where the change took place because other
* afterTextChanged() methods may already have made other changes
* and invalidated the offsets. But if you need to know here,
* you can use {@link Spannable#setSpan} in {@link #onTextChanged}
* to mark your place and then look up from here where the span
* ended up.
*/
public void afterTextChanged(Editable s);