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

在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. VS2012项目中使用CocoStudio相关文件的设置

    开发环境说明: win7  vs2012  coco2d-x 3.0 alpha1 cocos2d-x 3.0 alpha 1搭配CocoStudio使用,效果更佳.CocoStudio包含了游戏开发 ...

  2. 解决WPF中重载Window.OnRender函数失效问题

    今天实验一个绘图算法的时候,偶然发现重载Window.OnRender的方法是没有效果的. public partial class MainWindow : Window { public Main ...

  3. wpf 分别用前台和后台 两种方法 绘制矩形 填充

    xaml: <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft ...

  4. Ext表格控件

    表格控件其实也就是帮我们完成了数据的填充工作而已,具体的数据源.要显示的列,列的定制.数据源中的哪条数据显示在哪个列中等属性还是需要我们自己手动配置的,所以我们分下面几步来完成网格控件的数据绑定: 1 ...

  5. 搭建简单Ext

    一.EXT是什么? 1. Ext是一个Ajax框架,可以用来开发带有华丽外观的富客户端应用,使得我们的b/s应用更加具有活力及生命力,提高用户体验: 2. Ext是一个用javascript编写,与后 ...

  6. Java Collection 简介

    转自:http://skyuck.iteye.com/blog/526358 在 Java2中,有一套设计优良的接口和类组成了Java集合框架Collection,使程序员操作成批的数据或对象元素极为 ...

  7. sql 语句之 case

    case语句语法: --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END --Case搜索函数 CASE WHEN ...

  8. gpu和cpu区别

    GPU的功耗远远超过CPUCache, local memory: CPU > GPU Threads(线程数): GPU > CPURegisters: GPU > CPU 多寄存 ...

  9. Python学习(七)面向对象 ——类和实例

    Python 面向对象 —— 类和实例 类 虽然 Python 是解释性语言,但是它是面向对象的,能够进行对象编程.至于何为面向对象,在此就不详说了.面向对象程序设计本身就很值得深入学习,如要了解,请 ...

  10. Js的在线代码编辑器:CodeMirror

    github地址:https://github.com/codemirror/CodeMirror/tree/master/demo 里面包含需要的js.css文件以及大量的示例 官网:https:/ ...