TextWatcher()接口
当我们需要对一个编辑框(EditText)进行一定的限制,比如禁止输入某个字符,禁止错误的的格式等之类的操作时,就有必要用到EditText的时时监听,而TextWatcher就提供了这种功能。
详情请看下边的事例:
1. implements TextWatcher接口,重写相应的三个方法
2..在 void beforeTextChanged(CharSequence s, int start, int count,int after) { }
void onTextChanged(CharSequence s, int start, int before,int count) { }
void afterTextChanged(Editable s) { }中做操作
3. 给你的EditText 添加监听 et_keyword.addTextChangedListener(new editListener());
备注:参数
1.beforeTextChanged()
CharSequence s:文本改变之前的内容
int start:文本开始改变时的起点位置,从0开始计算
int count:要被改变的文本字数,即将要被替代的选中文本字数
int after:改变后添加的文本字数,即替代选中文本后的文本字数
2.onTextChanged()
CharSequence s:文本改变之后的内容
int start:文本开始改变时的起点位置,从0开始计算
int before:要被改变的文本字数,即已经被替代的选中文本字数
int count:改变后添加的文本字数,即替代选中文本后的文本字数
3.afterTextChanged()
Editable s:改变后的最终文本