博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的Android进阶之旅------>Android关于TextWatcher的初步了解
阅读量:7298 次
发布时间:2019-06-30

本文共 3017 字,大约阅读时间需要 10 分钟。

首先来看一下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 s,     * the count characters beginning at start     * are about to be replaced by new text with length after.     * It is an error to attempt to make changes to s from     * this callback.     */    public void beforeTextChanged(CharSequence s, int start,                                  int count, int after);    /**     * This method is called to notify you that, within s,     * the count characters beginning at start     * have just replaced old text that had length before.     * It is an error to attempt to make changes to s from     * this callback.     */    public void onTextChanged(CharSequence s, int start, int before, int count);    /**     * This method is called to notify you that, somewhere within     * s, the text has been changed.     * It is legitimate to make further changes to s 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);	}
看执行效果

 

 

 

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

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

  转载请保留原文地址

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

 

你可能感兴趣的文章
Python-函数
查看>>
全排列
查看>>
android音乐播放器(2)
查看>>
iOS presentedViewController的基本使用
查看>>
LNMP整合安装Redmine2.3实录
查看>>
易宝典文章——怎样管理Exchange Server 2013安全组
查看>>
erlang 简单例子的编译运行
查看>>
HyperV 中Windows Server 2012 非共享存储的在线迁移
查看>>
安装 CentOS 7 后必做的七件事
查看>>
myeclipse安装pydev
查看>>
【桌面虚拟化】之五PCoIP
查看>>
linux 监控CPU memory disk process 脚本
查看>>
Nginx启动脚本/etc/init.d/nginx
查看>>
Visual Studio 2017 离线安装方式
查看>>
枚举出局域网上所有网络资源
查看>>
Android深入浅出之Binder机制
查看>>
动态磁盘的管理
查看>>
zookeeper 集群安装(单点与分布式成功安装)
查看>>
python中list,dirt方法说明
查看>>
lamp环境一键部署(yum)
查看>>