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

在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. ORACLE FORMS PL/SQL PACKAGE SHOW TIPS WINDOW

    --在开发ORACLE FORM的程序中,要编写PL/SQL程序包产出异常情况时,直接在FORM弹出一个异常提示窗口 DECLARE BEGIN app_exception.raise_excepti ...

  2. python接口自动化10-token登录

    前言 有些登录不是用cookie来验证的,是用token参数来判断是否登录. token传参有两种一种是放在请求头里,本质上是跟cookie是一样的,只是换个单词而已:另外一种是在url请求参数里,这 ...

  3. 使用openURL实现程序间带参数跳转详解

    使用openURL实现程序间带参数跳转详解 实现的效果:有两款应用A与B,A打开B A --> B 1. 新建工程B,bundle ID为com.YouXianMing.B 建立一个URL 这么 ...

  4. 3D打印

    R=2.5的内径圆,需要R=2.2的圆柱才能吻合,否则插不进去.

  5. [Gradle] Gradle 构建 android 应用常见问题解决指南

    转载地址:http://www.cnblogs.com/youxilua/p/3348162.html 1: 使用最新的gradle android插件 以前我们写的时候会这么写 dependenci ...

  6. Python学习(五)函数 —— 内置函数 lambda filter map reduce

    Python 内置函数 lambda.filter.map.reduce Python 内置了一些比较特殊且实用的函数,使用这些能使你的代码简洁而易读. 下面对 Python 的 lambda.fil ...

  7. 完整的ASP.NET存储过程分页,排序,鼠标移至变色

    首先建立一个存储过程如下(MySQL数据库):CREATE DEFINER=`root`@`localhost` PROCEDURE `pagination`(       in tbName var ...

  8. 值得阅读的C语言开源项目代码

    本文地址:http://www.cnblogs.com/archimedes/p/c-opensource-project.html,转载请注明源地址. 本篇文章主要总结一些C开源项目,有些是很著名的 ...

  9. spring mvc--默认都使用了哪些bean

    在MVC中默认使用的bean都定义在了 org.springframework.web.servlet下的DispatcherServlet.properties 下载源文件后可查看到默认bean定义 ...

  10. JQMobile引入外部CSS,JS文件

    使用CDN <!DOCTYPE html> <html> <head> <title>html5</title> <meta name ...