TextView

2019-12-16  本文已影响0人  钦_79f7

方法

addTextChangedListener

TextView中关于文本内容的监听回调。由下面的源码及其命名方式addXXX,可以看出此方法不同于别的setXXXListener监听设置方式,此监听可以同时添加多个,所有的监听都没保存在mListeners的集合中。

private ArrayList<TextWatcher> mListeners;
/**
     * Adds a TextWatcher to the list of those whose methods are called
     * whenever this TextView's text changes.
     * <p>
     * In 1.0, the {@link TextWatcher#afterTextChanged} method was erroneously
     * not called after {@link #setText} calls.  Now, doing {@link #setText}
     * if there are any text changed listeners forces the buffer type to
     * Editable if it would not otherwise be and does call this method.
     */
    public void addTextChangedListener(TextWatcher watcher) {
        if (mListeners == null) {
            mListeners = new ArrayList<TextWatcher>();
        }

        mListeners.add(watcher);
    }

关于TextWacher

接口方法参数注释

```
/**
 * 监听textWatcher的基类
 * Created by sqq on 16/8/15.
 */
public class BaseTextWatcher implements TextWatcher {
    /**
     *
     * @param s 文本变化前的内容
     * @param start 新输入内容的起始下标
     * @param count 一般为0,下面的before
     * @param after 一般为1,即新输入的字符个数为1,等同于下面的after
     */
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    /**
     *
     * @param s 输入完成后字符串
     * @param start 新输入字符的起始下标
     * @param before 一般0,等同于上面的count
     * @param count 一般为1,等同于上面的after,新输入字符的个数
     */
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    /**
     *
     * @param s 
     */
    @Override
    public void afterTextChanged(Editable s) {
    }
}
```

相关资料收录

上一篇 下一篇

猜你喜欢

热点阅读