业无高卑志当坚,男儿有求安得闲?这篇文章主要讲述android之TextWatcher的监听相关的知识,希望能为你提供帮助。
以前用过android.text.TextWatcher来监听文本发生变化,但没有仔细去想它,今天兴致来了就发个疯来玩玩吧!
有点担心自己理解错,所以还是先把英文API解释给大家看看
1、什么情况下使用了?
When an object of a type is attached to an Editable, its methods will be called when the text is changed.
2、下面是它的三个抽象方法
/** *This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length *after. It is an error to attempt to make changes to s from this callback. */ public void beforeTextChanged(CharSequence s, int start, int count, int after)/** *This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had length *before. It is an error to attempt to make changes to s from this callback. */ public void onTextChanged(CharSequence s, int start, int before, int count)/** *This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate(合理) to make further changes to s *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 Spannable.setSpan in onTextChanged to mark your place *and then look up from here where the span ended up. */ public void afterTextChanged(Editable s)
3、英文看的不汪不楚的,还是实际来进行调试看结果(注意我是按他们触发的顺序来进行排列的)
我输入结果:aababoutabou 得到的结果: s:是改变前的结果,start是改变前的位置,count是减少时改变的个数,after是增加时改变的个数 beforeTextChanged(CharSequence s, int start, int count,int after) //null001 //a101 //ab203 //about410 s:是改变后的结果,start是改变前的位置,before是减少时改变的个数,count是增加时改变的个数 onTextChanged(CharSequence s, int start, int before,int count) //a001 //ab101 //about203 //abou410afterTextChanged(Editable s) //a //ab //about //abou
现在来看看怎么实现吧
首先是实现他的接口 implements TextWatcher
【android之TextWatcher的监听】这是他的监听事件addTextChangedListener(this)
推荐阅读
- Android 中的IPC机制
- Android轮播图
- 从Handler.post(Runnable r) ,Handler.sendEmptyMessage()梳理Android的消息机制(以及handler的内存泄露)
- Android触摸事件
- Android作业(Fragment的转换)
- 基于appium的移动端自动化测试,密码键盘无法识别问题
- Android图片加载框架最全解析,从源码的角度理解Glide的执行流程
- 国内外Android有名开发人员的Blog和git
- 平台即服务| PaaS