先翻译下Google文档对TextWatcher的介绍
| Method | Description |
|---|---|
| abstract void afterTextChanged(Editable s) | This method is called to notify you that, somewhere within s, the text has been changed. |
| abstract 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 are about to be replaced by new text with length after. |
| abstract void onTextChanged(CharSequence s, int start, int before, int count) | 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. |
| 方法 | 描述 |
|---|---|
| abstract void afterTextChanged(Editable s) | 调用此方法会通知你text文本已经变化了, text文本变为s(即变化后最终的text),如果你需要知道Text的那个地方变化了,可以在onTextChanged()中调用setSpan(object,int,int,int)来标记哪个地方变化了 |
| abstract void beforeTextChanged(CharSequence s, int start, int count, int after) | 调用此方法主要是通知你,文本s将要发生改变,改变前文本的长度为start,变化后,文本长度增加count,新的文本长度为after |
| abstract void onTextChanged(CharSequence s, int start, int before, int count) | 调用此方法会通知你,文本变为s,旧文本长度为before,旧文本的长度为start,新文本长度增加了count(一般使用start+count来从s中读取新增文本) |
想要进一步对新文本进行变化, 不要在onTextChanged()中, 而要选择在afterTextChanged(),但是不要让自己陷入循环中,因为任何一次文本的变化都会调用afterTextChange()方法而重置变化起始点,想要知道文本哪儿变化了可以考虑在onTextChange()中调用setSpan()给新增文本做标记.
本文详细介绍了Google文档中的TextWatcher方法,包括afterTextChanged、beforeTextChanged和onTextChanged的使用场景与实现细节。
5483

被折叠的 条评论
为什么被折叠?



