TextWatcher EditText 文本监听
来源:互联网 发布:直播平台主播大数据 编辑:程序博客网 时间:2024/05/01 22:27
对于一些需求,如非法字符限制(例如不允许输入#号,如果输入了#给出错误提示),做成动态判断更方便一些,而且容易扩展;
在Android里使用TextWatcher接口可以很方便的对EditText进行监听;TextWatcher中有3个函数需要重载:
<span class="kwrd">public</span> <span class="kwrd">void</span> beforeTextChanged(CharSequence s, <span class="kwrd">int</span> start, <span class="kwrd">int</span> count, <span class="kwrd">int</span> after); <span class="kwrd">public</span> <span class="kwrd">void</span> onTextChanged(CharSequence s, <span class="kwrd">int</span> start, <span class="kwrd">int</span> before, <span class="kwrd">int</span> count); <span class="kwrd">public</span> <span class="kwrd">void</span> afterTextChanged(Editable s);
从函数名就可以知道其意思,每当敲击键盘编辑框的文字改变时,上面的三个函数都会执行,beforeTextChanged可以给出变化之前的内容,onTextChanged和afterTextChanged给出追加上新的字符之后的文本;
- class MyWatcher implements TextWatcher{
- /**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.
- * 在s中,从start处开始的count个字符将要被长度为after的文本替代
- * s 为变化前的内容;
- * start 为开始变化位置的索引,从0开始计数;
- * count 为将要发生变化的字符数
- * after 为用来替换旧文本的长度,比如s由1变为12,after为1,由12变为1,after为0;
- */
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- // TODO Auto-generated method stub
- Log.i("test1", "before:s,"+s+",start,"+start+",count,"+count+",after,"+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
- * 在s中,从start处开始的count个字符刚刚替换了原来长度为before的文本
- * s 为变化后的内容;
- * start 为开始变化位置的索引,从0开始计数;
- * before 为被取代的老文本的长度,比如s由1变为12,before为0,由12变为1,before为1;
- * count 为将要发生变化的字符数
- */
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- // TODO Auto-generated method stub
- Log.i("test1", "ing:s,"+s+",start,"+start+",before,"+before+",count,"+count);
- }
- /**
- * This method is called to notify you that, somewhere within s, the text has been changed.
- */
- @Override
- public void afterTextChanged(Editable s) {
- // TODO Auto-generated method stub
- Log.i("test1", "after,s,"+s);
- }
- }
注册监听:
EditText mEditor = (EditText)findViewById(R.id.editor_input);mEditor.addTextChangedListener(mTextWatcher);
0 0
- TextWatcher EditText 文本监听
- 使用TextWatcher监听EditText
- TextWatcher 监听edittext
- EditText 监听-----TextWatcher
- EditText中的TextWatcher监听
- 监听EditText的变化--TextWatcher
- 关于EditText中的TextWatcher监听
- 关于EditText中的TextWatcher监听
- 关于EditText中的TextWatcher监听
- 关于EditText中的TextWatcher监听
- 详解EditText输入监听TextWatcher
- 使用TextWatcher监听EditText的文本变化之后动态改变EditText里面的内容
- Android:EditText动态输入监听TextWatcher
- 监听Edittext变化的TextWatcher用法
- 监听EditText文字变化,TextWatcher详解
- EditText实时监听TextWatcher的使用
- androidの监听EditView中的文本TextWatcher
- Android 文本监听接口TextWatcher详解
- 从网络异步加载listView中图片
- 什么时候程序员该跳槽了
- 没有搭讪的人生是不完整的
- Ubuntu上运行shell脚本报错:bad interpreter: No such file or directory
- mysql配置定时任务
- TextWatcher EditText 文本监听
- Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果
- 谷歌优化搜索算法 进一步打击盗版网站排名
- java 将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换
- Forms.Timer、Timers.Timer、Threading.Timer的研究
- dpkg: error: dpkg status database is locked by another process 解决方法
- 关于推进江苏省信息技术服务标准(ITSS)应用与推广工作的通知
- 黑马程序员---NSDictionary字典
- Java中单例模式实现方法