对于一些需求,如非法字符限制(例如不允许输入#号,如果输入了#给出错误提示),做成动态判断更方便一些,而且容易扩展;

在Android里使用TextWatcher接口可以很方便的对EditText进行监听;TextWatcher中有3个函数需要重载:

    public void beforeTextChanged(CharSequence s, int start,
int count, int after);
public void onTextChanged(CharSequence s, int start, int before, int count);
public void afterTextChanged(Editable s);

从函数名就可以知道其意思,每当敲击键盘编辑框的文字改变时,上面的三个函数都会执行,beforeTextChanged可以给出变化之前的内容,onTextChanged和afterTextChanged给出追加上新的字符之后的文本;

[java] view plaincopy

 
  1. classimplements         * beginning at start are about to be replaced by new text with length after.
  2. * 在s中,从start处开始的count个字符将要被长度为after的文本替代
  3. * s 为变化前的内容;
  4. * start 为开始变化位置的索引,从0开始计数;
  5. * count 为将要发生变化的字符数
  6. * after 为用来替换旧文本的长度,比如s由1变为12,after为1,由12变为1,after为0;
  7. */
  8. publicvoidintint int
  9. , +s++start++count++after);
  10. * This method is called to notify you that, within s, the count characters
  11. *  beginning at start have just replaced old text that had length before
  12. *  在s中,从start处开始的count个字符刚刚替换了原来长度为before的文本
  13. *  s 为变化后的内容;
  14. *  start 为开始变化位置的索引,从0开始计数;
  15. *  before 为被取代的老文本的长度,比如s由1变为12,before为0,由12变为1,before为1;
  16. *  count 为将要发生变化的字符数
  17. */
  18. publicvoidintint int
  19. , +s++start++before++count);
  20. * This method is called to notify you that, somewhere within s, the text has been changed.
  21. */
  22. publicvoid
  23. , +s);
  24. }

注册监听:

EditText mEditor = (EditText)findViewById(R.id.editor_input);
mEditor.addTextChangedListener(mTextWatcher);

TextWatcher的更多相关文章

  1. 使用EditText的addTextChangedListener(new TextWatcher())方法

    (转:http://www.apkbus.com/android-5257-1-14.html) 在使用EditText的addTextChangedListener(new TextWatcher( ...

  2. TextWatcher 编辑框监听器

    TextWatcher tw = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int sta ...

  3. Android TextWatcher监控EditText中的输入内容并限制其输入字符个数

    布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去观察输入框中输入的内容,作个笔记. 主布局: <?xm ...

  4. 监听EditText变化---TextWatcher 类用法详解

    http://www.cnblogs.com/yjing0508/p/5316985.html TextWatcher textWatcher = new TextWatcher() { @Overr ...

  5. 使用EditText+ListView并结合TextWatcher实现输入关键字筛选数据

    想必大家应该遇到过这样的情况,当点击Spinner控件后弹出的列表内容超多,一个一个滑动着去寻找所要的项很麻烦,尤其是当自己知道想要选择的内容,这时候如果我们只需要输入某些关键字,就可以讲上百条数据筛 ...

  6. 使用TextWatcher监听EditText变化

    public class MainActivity extends AppCompatActivity { private TextView mTextView; private EditText m ...

  7. AutoCompleteTextView与TextWatcher的结合

      /******************************************************************************************** * au ...

  8. TextWatcher编辑框监听器

    TextWatcher tw = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int sta ...

  9. AutoCompleteTextView 和 TextWatcher 详解

    TextWatcher 监测Edittext内容的变化------------------------------------------------------------------------- ...

  10. android UI-EditText的长度监听慎用TextWatcher

    在用户昵称的输入时,限定8个字符,本意是在输入超过8个时候,页面toast一个提示,就是下面的TextWatcher的监听,在afterTextChanged中处理. 原bug:huawei MT2- ...

随机推荐

  1. linux下使用crontab创建定时任务

    在linux中启动crontab服务: /etc/init.d/crond start crontab的命令格式 crontab -l 显示当前的crontab 文件(默认编写的crontab文件会保 ...

  2. Two PWM outputs from MCU combine to form a monotonic 16-bits DAC

    http://www.edn.com/design/analog/4329365/Combine-two-8-bit-outputs-to-make-one-16-bit-DAC

  3. WIFI模块 RTL8188EUS Realtek

    http://item.taobao.com/item.htm?spm=a230r.1.14.24.KnooKa&id=26119704895 W12 产品是一款采用国际先进台湾瑞昱Realt ...

  4. QMsgPack简介

    QMsgPack简介 首先,关于MessagePack协议,访问http://msgpack.org可以了解详细的格式约定及各种语言的实现. MessagePack协议号称比JSON快,但速度的快慢这 ...

  5. Android源码大放送之material design类型

    本文转载自:http://www.apkbus.com/android-243232-1-1.html 鉴于大家对源码的渴望,就算自己辛苦一点也要满足大家的需求,查看了几百个源码之后终于筛选出了这些精 ...

  6. android 水纹上涨与水滴滴下效果

    这两天项目比较紧,本来打算完成以后再写博客的,今天终于实现了一个炫的功能,怀着激动的心情,趁热打铁,把项目经验记录一下,效果图如下:  对功能的几点说明: 1.圆形边框旋转 2.水纹上涨 3.水滴滴下 ...

  7. springboot2.X整合mybatis

    github地址:https://github.com/BenchChen/springboot 1) 创建springboot-maven项目,并修改pom文件 <?xml version=& ...

  8. ReflectionToStringBuilder使用

    <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3 ...

  9. SharePoint 在中国为什么吃不开?

    1.功能不完善,二次开发费时费力?2.没有完整的解决方案,案例太少,很多企业不敢下手?3.国内人员,操作性比较差,不愿意接受SharePoint的操作方式?4.价格太贵,小型企业花费性价比不高?5.界 ...

  10. nolock的使用

    在SQL Server 2005数据库查询时,为了提高查询的性能,我们往往会在表后面加一个nolock,或者是with(nolock),让数据库在查询时不锁定表,从而提高查询的速度.本文我们就介绍SQ ...