首先来看一下TextWatcher的源代码

package android.text;

/**
* When an object of a type is attached to an Editable, its methods will
* be called when the text is changed.
*/
public interface TextWatcher extends NoCopySpan {
/**
* This method is called to notify you that, within <code>s</code>,
* the <code>count</code> characters beginning at <code>start</code>
* are about to be replaced by new text with length <code>after</code>.
* It is an error to attempt to make changes to <code>s</code> from
* this callback.
*/
public void beforeTextChanged(CharSequence s, int start,
int count, int after);
/**
* This method is called to notify you that, within <code>s</code>,
* the <code>count</code> characters beginning at <code>start</code>
* have just replaced old text that had length <code>before</code>.
* It is an error to attempt to make changes to <code>s</code> from
* this callback.
*/
public void onTextChanged(CharSequence s, int start, int before, int count); /**
* This method is called to notify you that, somewhere within
* <code>s</code>, the text has been changed.
* It is legitimate to make further changes to <code>s</code> from
* this callback, but be careful not to get yourself into an infinite
* loop, because any changes you make will cause this method to be
* called again recursively.
* (You are not told where the change took place because other
* afterTextChanged() methods may already have made other changes
* and invalidated the offsets. But if you need to know here,
* you can use {@link Spannable#setSpan} in {@link #onTextChanged}
* to mark your place and then look up from here where the span
* ended up.
*/
public void afterTextChanged(Editable s);
}

下面通过通过一个小实例来学习TextWatcher的相关用法

实现该接口

TextWatcher mTextWatcher = new TextWatcher() {
private CharSequence temp;
private int editStart;
private int editEnd; @Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2,
int arg3) {
temp = s;
} @Override
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
mDetailView.setText(s);
} @Override
public void afterTextChanged(Editable s) {
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
if (temp.length() > 11) {
Toast.makeText(MainActivity.this, "你输入的字数已经超过了限制!",
Toast.LENGTH_SHORT).show();
s.delete(editStart - 1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
};

再注册这个监听

        TextView mDetailView;
EditText mEditText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDetailView = (TextView) findViewById(R.id.mDetailView);
mEditText = (EditText) findViewById(R.id.mEditText);
mEditText.addTextChangedListener(mTextWatcher);
}

看运行效果



                            ====================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

====================================================================================

 

最新文章

  1. 如何配置Linux系统的网络IP地址
  2. HDU3038 How Many Answers Are Wrong[带权并查集]
  3. php5 数据类型
  4. 数据格式json讲解
  5. JavaScript Patterns 4.9 Configuration Objects
  6. Unity手游之路&lt;四&gt;3d旋转-四元数,欧拉角和变幻矩阵
  7. [转]Could not load file or assembly &#39;System.Core, Version=2.0.5.0 和autofac冲突的问题
  8. leetcode第一刷_Interleaving String
  9. java工程师联通XX面试题目
  10. mysql语句sum求和为null的问题
  11. 兼容ie浏览器的placeholder的几种方法
  12. 异常处理第三讲,SEH(结构化异常处理),异常展开问题
  13. CMake基本语法
  14. JAVA数组的定义以及使用1
  15. tensorflow 升级到1.9-rc0,tensorboard 报错:TypeError: GetNext() takes exactly 1 argument (2 given)
  16. vue 移动端日期选择组件 vue-mobile-calendar
  17. .Net开源myrtille远程连接服务(支持SSH)
  18. svn服务器搭建及使用(二)
  19. java 中的好东西 jackson
  20. Golang教程:包

热门文章

  1. EffectiveJava(13)使类和成员的可访问性最小化
  2. HTML5 Canvas 奔跑的小狗
  3. PS如何拉倒影效果
  4. 24.JAVA编程思想——违例差错控制
  5. Nginx:HTTP过滤模块
  6. Eclipse配色利器
  7. Android XMPP 即时通讯
  8. PHP面试题及答案解析(8)—PHP综合应用题
  9. 网络相关系列之四:数据解析之SAX方式解析XML数据
  10. 【COCOS2DX-LUA 脚本开发之四】