Android EditText TextWatcher 回调方法参数说明

本文详细解析了Android中EditText的TextWatcher监听器的三个回调方法beforeTextChanged、onTextChanged和afterTextChanged,通过实验解释了它们的参数含义及在不同操作下的行为表现。
摘要由CSDN通过智能技术生成

有些时候需要用到TextWatcher来监听EditText的内容变化,

而TextWatcher有三个回调方法,

这三个回调方法在Android的API Document中说的并不是很容易理解,

我尝试用我的思路以及试验解释一下这三个回调方法的参数。


// **************************************************************

// 这一部分比较枯燥可以跳过,直接读试验部分

// 试验部分的结果会对比这一部分的Javadoc来确认对试验结果理解的正确性


三个回调方法分别是 (按回调的先后顺序):

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 -- Javadoc:

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 lengthafter. It is an error to attempt to make changes tos from this callback.



onTextChanged -- Javadoc:

This method is called to notify you that, within s, the count characters beginning at start have just replaced old text that had lengthbefore. It is an error to attempt to make changes tos from this callback.


afterTextChanged -- Javadoc:

This method is called to notify you that, somewhere withins, the text has been changed. It is legitimate to make further changes tos 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 useSpannable.setSpan inonTextChanged to mark your place and then look up from here where the span ended up.

// **************************************************************


// **************************************************************

// 试验部分开始

// 试验1:

// Part 1: 粘贴 “cmd” 到EditText

// Part 2: 选中“cm”并删除


首先看 Part 1:


cmd

s start count
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值