onTextChanged参数解释及实现EditText字数监听

由于最近做项目要检测EditText中输入的字数长度,从而接触到了Android中EditText的监听接口,TextWatcher。
它有三个成员方法,第一个after很简单,这个方法就是在EditText内容已经改变之后调用,重点看下面两个方法:

1
beforeTextChanged(CharSequence s, int start, int count, int after)

这个方法是在Text改变之前被调用,它的意思就是说在原有的文本s中,从start开始的count个字符将会被一个新的长度为after的文本替换,注意这里是将被替换,还没有被替换

1
onTextChanged(CharSequence s, int start, int before, int count)

这个方法是在Text改变过程中触发调用的,它的意思就是说在原有的文本s中,从start开始的count个字符替换长度为before的旧文本,注意这里没有将要之类的字眼,也就是说一句执行了替换动作

可能说起来比较抽象,我举个简单的例子,比如说我们监听一个EditText,默认开始的时候EditText中没有文本,当我们输入LOVE四个字母的时候,在打印信息中我输出各个参数看一下参数的变化。

1
2
3
4
5
6
7
8
9
10
11
12
10-18 16:40:21.528: D/Debug(4501): beforeTextChanged 被执行---->s=----start=0----after=1----count=0
10-18 16:40:21.528: D/Debug(4501): onTextChanged 被执行---->s=L----start=0----before=0----count=1
10-18 16:40:21.532: D/Debug(4501): afterTextChanged 被执行---->L
10-18 16:40:29.304: D/Debug(4501): beforeTextChanged 被执行----> s=L----start=1----after=1----count=0
10-18 16:40:29.308: D/Debug(4501): onTextChanged 被执行---->s=LO----start=1----before=0----count=1
10-18 16:40:29.308: D/Debug(4501): afterTextChanged 被执行---->LO
10-18 16:40:32.772: D/Debug(4501): beforeTextChanged 被执行---->s=LO----start=2----after=1----count=0
10-18 16:40:32.772: D/Debug(4501): onTextChanged 被执行---->s=LOV----start=2----before=0----count=1
10-18 16:40:32.776: D/Debug(4501): afterTextChanged 被执行---->LOV
10-18 16:40:34.772: D/Debug(4501): beforeTextChanged 被执行---->s=LOV----start=3----after=1----count=0
10-18 16:40:34.772: D/Debug(4501): onTextChanged 被执行---->s=LOVE----start=3----before=0----count=1
10-18 16:40:34.776: D/Debug(4501): afterTextChanged 被执行---->LOVE

通过上面的打印信息我们可以发现在输入L之前beforeTextChanged被执行,s为空,所以s输入空,start=0,也就是从位置0开始,count=0,也就是0个字符将会被替换,after=1,也就是说0个字符将会被一个新的长度为after=1的文本(也就是L)替换。

当输入发生改变的时候onTextChanged被执行,此时s=L也就是输入的字母L,从start=0开始,count=1个字符替换了长度为before=0的旧文本。通俗点将就是字母L从位置0开始替换了原来的空文本,下面的就可以依次类推了。那么我们如何利用这个接口监听EditText的文本变化来实现限制输入字数的功能呢,我相信大家都有自己的想法了,这里我给出自己的一个简单实现,主要代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
source_des.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private int selectionStart;
private int selectionEnd;

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged 被执行----> s=" + s + "----start="+ start
+ "----before="+before + "----count" +count); temp = s;
}

public void beforeTextChanged(CharSequence s, int start, int count,int after) {
Log.d(TAG, "beforeTextChanged 被执行---->s=" + s+"----start="+ start
+ "----after="+after + "----count" +count);
}

public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged 被执行---->" + s);
selectionStart = source_des.getSelectionStart();
selectionEnd = source_des.getSelectionEnd();
if (temp.length() > MAX_LENGTH) {
Toast.makeText(MainActivity.this, "只能输入九个字",
Toast.LENGTH_SHORT).show();
s.delete(selectionStart - 1, selectionEnd);
int tempSelection = selectionEnd;
source_des.setText(s);
source_des.setSelection(tempSelection);
}
}
});

如大家有什么疑问,欢迎交流