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

在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. gitignore / Delphi.gitignore

    https://github.com/github/gitignore/blob/master/Delphi.gitignore *.dcu *.~*~ *.local *.identcache __ ...

  2. [Win32]创建模态窗口

    http://www.cnblogs.com/zplutor/archive/2011/02/20/1958973.html 在Win32编程中,如果要显示一个模态窗口,一般是先创建对话框模板,然后使 ...

  3. 按字母顺序排列的IDC函数列表

    http://www.2cto.com/shouce/ida/162.htm 按字母顺序排列的IDC函数列表 下面是函数描述信息中的约定: 'ea' 线性地址 'success' 0表示函数失败:反之 ...

  4. 多线程间通信之AutoResetEvent和ManualResetEvent的原理分析和开发示例

    AutoResetEvent 允许线程通过发信号互相通信. 通常,当线程需要独占访问资源时使用该类. 线程通过调用 AutoResetEvent 上的 WaitOne 来等待信号. 如果 AutoRe ...

  5. 简化delegate写法

    标准的写法 空 简化后的宏 /**************************************************************/ // delegate 托付 /* #de ...

  6. 我的sourceinsight的配置

    下面是我的sourceinsight的配置,点击下面的链接,下载*.em文件,将他们添加到Base工程,设置相应的快捷键即可,或者导入下载的配置文件. http://pan.baidu.com/s/1 ...

  7. mormot解析天气预报JSON数据

    mormot解析天气预报JSON数据 uses SynCommons; constjson2 = '{' + #13#10 +'"error":0,' + #13#10 +'&qu ...

  8. React的第一个例子

    准备: 官网:https://facebook.github.io/react/downloads.html Github地址:https://github.com/facebook/react 首先 ...

  9. 关于Go语言,自己定义结构体标签的一个妙用.

    在Go中首字母大写和小写,决定着这此变量能否被外部调用, 比如:在使用标准库的json编码自定一的结构的时候: <pre style="margin-top: 0px; margin- ...

  10. pytest文档8-html报告报错截图+失败重跑

    前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...