onTextChanged参数解释及实现EditText字数监听
http://www.picksomething.cn/?p=34
由于最近做项目要检测EditText中输入的字数长度,从而接触到了Android中EditText的监听接口,TextWatcher。
它有三个成员方法,第一个after很简单,这个方法就是在EditText内容已经改变之后调用,重点看下面两个方法:
beforeTextChanged(CharSequence s, int start, int count, int after)
这个方法是在Text改变之前被调用,它的意思就是说在原有的文本s中,从start开始的count个字符将会被一个新的长度为after的文本替换,注意这里是将被替换,还没有被替换。
onTextChanged(CharSequence s, int start, int before, int count)
这个方法是在Text改变过程中触发调用的,它的意思就是说在原有的文本s中,从start开始的count个字符替换长度为before的旧文本,注意这里没有将要之类的字眼,也就是说一句执行了替换动作。
可能说起来比较抽象,我举个简单的例子,比如说我们监听一个EditText,默认开始的时候EditText中没有文本,当我们输入LOVE四个字母的时候,在打印信息中我输出各个参数看一下参数的变化。
- ::21.528: D/Debug(): beforeTextChanged 被执行----> s=----start=----after=----count=
- ::21.528: D/Debug(): onTextChanged 被执行---->s=L----start=----before=----count=
- ::21.532: D/Debug(): afterTextChanged 被执行---->L
- ::29.304: D/Debug(): beforeTextChanged 被执行----> s=L----start=----after=----count=
- ::29.308: D/Debug(): onTextChanged 被执行---->s=LO----start=----before=----count=
- ::29.308: D/Debug(): afterTextChanged 被执行---->LO
- ::32.772: D/Debug(): beforeTextChanged 被执行----> s=LO----start=----after=----count=
- ::32.772: D/Debug(): onTextChanged 被执行---->s=LOV----start=----before=----count=
- ::32.776: D/Debug(): afterTextChanged 被执行---->LOV
- ::34.772: D/Debug(): beforeTextChanged 被执行----> s=LOV----start=----after=----count=
- ::34.772: D/Debug(): onTextChanged 被执行---->s=LOVE----start=----before=----count=
- ::34.776: D/Debug(): afterTextChanged 被执行---->LOVE
通过上面的打印信息我们可以发现在输入L之前beforeTextChanged
被执行,s为空,所以s输入空,start=0,也就是从位置0开始,count=0,也就是0个字符将会被替换,after=1,也就是说0个字符将会被一个新的长度为after=1的文本(也就是L)替换。
当输入发生改变的时候onTextChanged
被执行,此时s=L也就是输入的字母L,从start=0开
始,count=1个字符替换了长度为before=0的旧文本。通俗点将就是字母L从位置0开始替换了原来的空文本,下面的就可以依次类推了。那么我们
如何利用这个接口监听EditText的文本变化来实现限制输入字数的功能呢,我相信大家都有自己的想法了,这里我给出自己的一个简单实现,主要代码如
下:
source_des.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private int selectionStart;
private int selectionEnd; @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d(TAG, "onTextChanged 被执行---->s=" + s + "----start="+ start
+ "----before="+before + "----count" +count); temp = s;
} public void beforeTextChanged(CharSequence s, int start, int count,int after) {
Log.d(TAG, "beforeTextChanged 被执行----> s=" + s+"----start="+ start
+ "----after="+after + "----count" +count);
} public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged 被执行---->" + s);
selectionStart = source_des.getSelectionStart();
selectionEnd = source_des.getSelectionEnd();
if (temp.length() > MAX_LENGTH) {
Toast.makeText(MainActivity.this, "只能输入九个字",
Toast.LENGTH_SHORT).show();
s.delete(selectionStart - 1, selectionEnd);
int tempSelection = selectionEnd;
source_des.setText(s);
source_des.setSelection(tempSelection);
}
}
});
如大家有什么疑问,欢迎交流。
小结:多总结。
onTextChanged参数解释及实现EditText字数监听的更多相关文章
- 安卓onTextChanged参数解释及实现EditText字数监听 Editable使用
原作者部分修改部分 补充部分 补充部分2 补充部分3 补充部分4 Editable 尊重原作者:此篇文章是借鉴原作者地址 的博文 并进行修改和增加补充说明,我只是补充和修改: 我感觉这篇文章经过我的补 ...
- Android addTextChangedListener(文本监听)参数解释及实现EditText字数监听
由于最近做项目要检测EditText中输入的字数长度,从而接触到了Android中EditText的监听接口,TextWatcher.它有三个成员方法,第一个after很简单,这个方法就是在EditT ...
- Android EditText内容监听
监听 EditText的内容变化,作出对应的处理. MainActivity.class package com.example.edittextdemo; import android.app.Ac ...
- Android 实现对多个EditText的监听
create_account=(EditText)findViewById(R.id.create_account); create_password=(EditText)findViewById(R ...
- EditText获取焦点监听事件_EditText获取和失去焦点操作
今天在做搜索框的时候.遇到需要获取焦点之后做一些事情.实现方法也很简单.那就是绑定OnFocusChangeListener事件.实现 onFocusChange(View v, boolean ha ...
- textarea增加字数监听且高度自适应(兼容IE8)
1.封装方法: var textareaListener = { /*事件监听器兼容 * *attachEvent——兼容:IE7.IE8:不兼容firefox.chrome.IE9.IE10.IE1 ...
- 监听EditText的变化
http://liangruijun.blog.51cto.com/3061169/729505 之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下 ...
- memcached参数解释及常用命令
一.执行 memcached -h 会显示所有的参数项,对应的中文解释如下: -p <num> 监听的TCP端口(默认: 11211) -U <num> 监 ...
- Android开发之基于监听的事件处理
在Android 应用开发过程中,常用监听事件如下:(1) ListView事件监听setOn ItemSelectedListener:鼠标滚动时触发setOnItemClickListener: ...
随机推荐
- bzoj1231
看到n<=16不难想到状压dp 我们用二进制表示前x个位置,哪些牛被已经被选过了 这里我们可以通过穷举二进制数的顺序来转移 所以二维就够了 ..] of longint; f:.. sh ...
- BZOJ_3224_普通平衡树_(Treap)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3224 Treap模板题.支持如下几种操作: 1.插入; 2.删除; 3.rank(x); 4. ...
- Node.js权威指南 (13) - 数据库访问
13.1 在MongoDB数据库中存取数据 / 360 13.1.1 MongoDB概述 / 360 13.1.2 安装MongoDB数据库 / 360 13.1.3 安装MongoDB包 / 361 ...
- eclipse环境NDK问题汇总
1. 配置NDK路径设置 可以在cygwin中通过vim修改,也可以在windows安装目录中修改 home\<你的用户名>\.bash_profile 文件中最后添加环境变量 NDK=/ ...
- js中的时间转换—毫秒转换成日期时间
转自:http://www.javascript100.com/?p=181 前几天,在项目中遇到js时间增加问题,要将js毫秒时间转换成日期时间 var oldTime = (new Date(&q ...
- HDJ -- 1022
#include<iostream> #include<cstdio> #include<string> #define MAXN 10 using namespa ...
- Mac下go语言goclipse插件安装部署
Try using this URL as a Eclipse Software Site:https://raw.githubusercontent.com/GoClipse/goclipse.gi ...
- 理解newid()和newsequentialid()
原文地址:http://blog.csdn.net/xushichang/article/details/4390957 1.:newsequentialid 函数比起 newid 函数最大的好处是: ...
- JavaScript高级程序设计61.pdf
JSON对象 早期的JSON解析器就是使用JavaScript的eval()函数,ECMAScript5对解析JSON的行为做出了规定,定义了全局对象JSON. JSON对象有2个方法:stringi ...
- Java 并发包中的读写锁及其实现分析
1. 前言 在Java并发包中常用的锁(如:ReentrantLock),基本上都是排他锁,这些锁在同一时刻只允许一个线程进行访问,而读写锁在同一时 刻可以允许多个读线程访问,但是在写线程访问时,所有 ...