当前位置:首页 > Android > 正文内容

onTextChanged参数解释及实现EditText字数监听

jsc10年前 (2016-04-01)Android3530

由于最近做项目要检测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四个字母的时候,在打印信息中我输出各个参数看一下参数的变化。

10-18 16:40:21.528: D/Debug(4501): beforeTextChanged 被执行----> s=----start=0----after=1----count=0
10-18 16:40:21.528: D/Debug(4501): onTextChanged 被执行---->s=L----start=0----before=0----count=1
10-18 16:40:21.532: D/Debug(4501): afterTextChanged 被执行---->L
10-18 16:40:29.304: D/Debug(4501): beforeTextChanged 被执行----> s=L----start=1----after=1----count=0
10-18 16:40:29.308: D/Debug(4501): onTextChanged 被执行---->s=LO----start=1----before=0----count=1
10-18 16:40:29.308: D/Debug(4501): afterTextChanged 被执行---->LO
10-18 16:40:32.772: D/Debug(4501): beforeTextChanged 被执行----> s=LO----start=2----after=1----count=0
10-18 16:40:32.772: D/Debug(4501): onTextChanged 被执行---->s=LOV----start=2----before=0----count=1
10-18 16:40:32.776: D/Debug(4501): afterTextChanged 被执行---->LOV
10-18 16:40:34.772: D/Debug(4501): beforeTextChanged 被执行----> s=LOV----start=3----after=1----count=0
10-18 16:40:34.772: D/Debug(4501): onTextChanged 被执行---->s=LOVE----start=3----before=0----count=1
10-18 16:40:34.776: D/Debug(4501): 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); 
        } 
    } 
});


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://jsc0.com/post/58.html

标签: EditText
分享给朋友:

“onTextChanged参数解释及实现EditText字数监听” 的相关文章

实现应用程序只有在第一次启动时显示引导界面

第一次安装启动:启动页--->导航页-->主页面之后启动:启动页-->主页面实现的原理就是:在启动页面用做一个文件保存的状态,保存程序是不是第一次启动的状态。因为只是要保存一个状态,我们将这个程序是第一次打开就将他设为true,当他进入 主页面之后将他的状态未为false,因为都...

Android ImageView的scaleType属性与adjustViewBounds属性

Android ImageView的scaleType属性与adjustViewBounds属性

ImageView的scaleType的属性有好几种,分别是matrix(默认)、center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY android:sca...

Android中从SD卡中/拍照选择图片并进行剪裁的方法

Android中从SD卡中/拍照选择图片并进行剪裁的方法

效果图: 下面是代码的部分,部分是从网路上摘录的,自己整理后当做工具类使用   配置文件:布局很简单,一个ImageButton和一个Button,点击都可以实现图像选择的功能,具体的实现根据大家在实际中...

Android SharedPreferences PreperenceScreen 偏好数据存取

SharedPreferences是一个接口,程序是无法创建SharedPreferences实例的,1、Context.getSharedPreferences(String name,int mode)来得到一个指定name的SharedPreferences实例2、PreferenceMana...

android 定时循环执行任务

一、执行循环的函数如下:private Handler handler = new Handler();         private Runnable&...

Sipnner点击相同Item不响应的解决方法

Spinner,两次点击同一个item的时候,第二次会不响应OnItemSelect事件。解决方法:@Override public void onItemSelected(AdapterView<?> arg0, View arg1,...