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

在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. 树莓派(Debian)系统开启iptables的raw表实现日志输出

    说明:可能Debian默认不开启iptables的raw表,所以无法通过其实现日志跟踪. 日志跟踪:http://www.cnblogs.com/EasonJim/p/8413563.html 解决方 ...

  2. 安卓之service简单介绍

    一 什么是Service 二 如何使用Service 三 Service的生命周期   一 什么是Service Service,看名字就知道跟正常理解的“服务”差不多,后台运行,可交互这样的一个东西 ...

  3. 线性稳压器 (LDO)

    线性稳压器 (LDO) 当输出电压低于输入电压时,必须从负载电源和压降比来考虑要采用线性稳压器 (LDO) 或降压转换器 (Buck) 低压降线性稳压器 (LDO) 适合用在需求低噪讯.低电流和低压降 ...

  4. Programmable current source requires no power supply

    Engineering labs are usually equipped with various power supplies, voltmeters, function generators, ...

  5. mysql知识点(二)

    1.什么叫聚集索引和非聚集索引? 答: 聚集索引:该索引中键值的逻辑顺序决定了表中相应行的物理顺序.      聚集索引确定表中数据的物理顺序.聚集索引类似于电话簿,后者按姓氏排列数据.由于聚集索引规 ...

  6. Nuget server on IIS6 returns 404

    Nuget server on IIS6 returns 404 when downloading package after upgrade 2011年9月2日 8:03:30 (GMT Dayli ...

  7. 运行Hadoop的示例程序WordCount-Running Hadoop Example

      In the last post we've installed Hadoop 2.2.0 on Ubuntu. Now we'll see how to launch an example ma ...

  8. Power Desginer系列02【转载】

    在概念模型中主要有以下几个操作和设置的对象:实体(Entity).实体属性 (Attribute).实体标识(Identifiers).关系(Relationship).继承(Inheritance) ...

  9. Dede(织梦) CMS SQL Injection Vulnerability

    测试方法: @Sebug.net   dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! # Dede Cms All Versions Sql Vulnerability ...

  10. 【Scroller】scrollTo scrollBy startScroll computeScroll 自定义ViewPage 简介 示例

    简介 android.widget.Scroller是用于模拟scrolling行为,它是scrolling行为的一个帮助类.我们通常通过它的 startScroll 函数来设置一个 scrollin ...