我是靠谱客的博主 英勇世界,这篇文章主要介绍Android addTextChangedListener方法介绍,现在分享给大家,希望可以做个参考。

文本变化监听器addTextChangedListener中TextWatcher方法三个方法意义

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
EditText editText = findViewById(R.id.et_test); editText.addTextChangedListener(new TextWatcher() { // charSequence为在你按键之前显示的字符串 start为新字符串与charSequence开始出现差异的下标 count表示原字符串的count个字符 after表示将会被after个字符替换 @Override public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) { Log.i(TAG, "beforeTextChanged: charSequence=" + charSequence + ", start=" + start + ", count=" + count + ", after=" + after); } // 按键之前字符串的start位置的before个字符已经被count个字符替换形成新字符串charSequence @Override public void onTextChanged(CharSequence charSequence, int start, int before, int count) { Log.i(TAG, "onTextChanged: charSequence=" + charSequence + ", start=" + start + ", before=" + before + ", count=" + count); } // afterTextChanged中 editable为EditText显示的内容 @Override public void afterTextChanged(Editable editable) { Log.i(TAG, "afterTextChanged: editable=" + editable); } });

通过上面注释已经可以理解了,此处附上测试log

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 输入a com.nan.myapplication I/breeze: beforeTextChanged: s=, start=0, count=0, after=1 com.nan.myapplication I/breeze: onTextChanged: s=a, start=0, before=0, count=1 // 继续输入b com.nan.myapplication I/breeze: beforeTextChanged: s=a, start=1, count=0, after=1 com.nan.myapplication I/breeze: onTextChanged: s=ab, start=1, before=0, count=1 // 继续输入GG com.nan.myapplication I/breeze: beforeTextChanged: s=ab, start=2, count=0, after=2 com.nan.myapplication I/breeze: onTextChanged: s=abGG, start=2, before=0, count=2 // 删除最后一个G com.nan.myapplication I/breeze: beforeTextChanged: s=abGG, start=3, count=1, after=0 com.nan.myapplication I/breeze: onTextChanged: s=abG, start=3, before=1, count=0 // 将bG选中替换为嘿嘿 com.nan.myapplication I/breeze: beforeTextChanged: s=abG, start=1, count=2, after=2 com.nan.myapplication I/breeze: onTextChanged: s=a嘿嘿, start=1, before=2, count=2

此处省略了afterTextChanged中的log,这个方法没有任何疑问,能少一句log就少一句,清爽!

最后

以上就是英勇世界最近收集整理的关于Android addTextChangedListener方法介绍的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部