Android TextWatcher三个回调以及监听EditText的输入案例详解
作者:LittleFogCat 发布时间:2022-06-04 10:22:29
TextWatcher是一个监听字符变化的类。当我们调用EditText的addTextChangedListener(TextWatcher)方法之后,就可以监听EditText的输入了。
在new出一个TextWatcher之后,我们需要实现三个抽象方法:
beforeTextChanged
onTextChanged
afterTextChanged
看下beforeTextChanged的注释:
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.
不知道你看懂没有,可能是因为英文水平的问题,我反正是看的很含糊不清。
既然这样,那最好的办法就是自己打个log试一下了。
EditText et = (EditText) findViewById(R.id.et);
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG, "beforeTextChanged: s = " + s + ", start = " + start +
", count = " + count + ", after = " + after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged: s = " + s + ", start = " + start +
", before = " + before + ", count = " + count);
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged: " + s);
}
};
et.addTextChangedListener(watcher);
首先输入abc123
然后在c和1之间添加一个0,现在文字变成了abc0123,再看下LOGCAT的显示:
再把0给删掉,log如下:
把0替换成12:
通过Log,结合Android源码注释,可以得出结论:
beforeTextChanged(CharSequence s, int start, int count, int after)
s: 修改之前的文字。
start: 字符串中即将发生修改的位置。
count: 字符串中即将被修改的文字的长度。如果是新增的话则为0。
after: 被修改的文字修改之后的长度。如果是删除的话则为0。onTextChanged(CharSequence s, int start, int before, int count)
s: 改变后的字符串
start: 有变动的字符串的序号
before: 被改变的字符串长度,如果是新增则为0。
count: 添加的字符串长度,如果是删除则为0。afterTextChanged(Editable s)
s: 修改后的文字
Tips:
**需要特别注意的是,在TextWatcher中setText要小心不要陷入死循环。**即:
文字改变->watcher接收到通知->setText->文字改变->watcher接受到通知->…
在watcher中使用setText的解决方案
关于这三个回调方法调用的时机,可以参考这篇文章
当对EditText使用setText之后,记得再使用setSelection()来设置光标位置。
一个完整的TextWatcher例子:
// 用于屏蔽空格输入的TextWatcher
TextWatcher watcher = new TextWatcher() {
private String mBefore;// 用于记录变化前的文字
private int mCursor;// 用于记录变化时光标的位置
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
mBefore = s.toString();
mCursor = start;
Log.d(TAG, "beforeTextChanged: " + s + ", " + mCursor);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged: " + s);
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged: " + s);
// 这里图方便,是在afterTextChanged判断是否输入之后含有空格,并不是最好的方案
// 理论上应该在onTextChanged中判断输入内容
if (s.toString().contains(" ")) {
Log.i(TAG, "onTextChanged: 包含空格, 设置" + mBefore);
et.removeTextChangedListener(this);
et.setText(mBefore);
et.addTextChangedListener(this);
et.setSelection(mCursor);
}
}
};
et.addTextChangedListener(watcher);
来源:https://blog.csdn.net/zengsidou/article/details/78665301
猜你喜欢
- Okhttp 处理了很多网络疑难杂症,比如从很多常用的连接问题中自动恢复。如果你服务器配置了多个IP地址,当一个IP地址连接失败后Okhtt
- 本文实例讲述了C#编程实现取整和取余的方法。分享给大家供大家参考,具体如下:"%"为取余号,不用多说。 "/&
- 方法重载概述方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法互相构成重载* 多个方法在同一个类中* 多个放方法具有相同方
- 昨天在与对端系统调接口的时候,对端系统对我们传过去的json串老是处理不了,后来查原因是应为我们传过去的json串里有json对象数组,因为
- 前言今天起床,拿起手机开机第一时间当然是打开微信了,左右滑动Viewpager,发现它使用了一种叫惰性加载,或者说懒加载(lazy-load
- 本文介绍了springcloud Feign的Hystrix支持,分享给大家,具体如下:一、Feign client中加入Hystrix的f
- 【1】首先我们定义一段假数据,这里以一个string为例字static void Main(string[] args){string da
- 前言本文主要介绍了关于java结合keytool实现非对称签名和验证的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍
- 60年代,在OS中能拥有资源和独立运行的基本单位是进程,然而随着计
- 前言这段时间比较闲,就看起了jdk源码。一般的一个高级开发工程师, 能阅读一些源码对自己的提升还是蛮大的。本文总结了一些JDK源码中的“小技
- 其他人写的都是调用非托管kernel32.dll。我也用过 但是感觉兼容性有点不好 有时候会出现编码错误,毕竟一个是以前的系统一个是现在的系
- * String类是不可变类,只要对String进行修改,都会导致新的对象生成。* StringBuffer和StringBuilder都是
- 通过java的File类创建临时文件,然后在程序退出时自动删除临时文件。下面将通过创建一个JFrame界面,点击创建按钮在当前目录下面创建t
- ImGUI 它是与平台无关的C++轻量级跨平台图形界面库,没有任何第三方依赖,可以将ImGUI的源码直接加到项目中使用,该框架通常会配合特定
- 目录前言:对文章出现的一些名词进行解释一、插入排序1.基本思想2.直接插入排序3.希尔排序(缩小增量排序)二、选择排序1.基本思想2.直接选
- 简介TreeMap使用红黑树存储元素,可以保证元素按key值的大小进行遍历。继承体系TreeMap实现了Map、SortedMap、Navi
- 本文以一个完整实例讲述了C#水印图片的生成方法。是非常实用的技巧。分享给大家供大家参考。具体实例代码如下:/** * 使用说明:
- 这篇文章主要介绍了通过实例了解spring使用构造器注入的原因,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 本文实例为大家分享了C#实现简单的聊天窗体的具体代码,供大家参考,具体内容如下一、要使用(学习)到的知识点1、textBox控件(1)功能:
- 本文实例为大家分享了Java NIO实现多人聊天室的具体代码,供大家参考,具体内容如下1. 服务器端代码ChatServer类:packag