Android的TextWatcher.afterTextChanged VS TextWatcher.onTextChanged [英] Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

查看:389
本文介绍了Android的TextWatcher.afterTextChanged VS TextWatcher.onTextChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我应该用什么情况下 afterTextChanged 而不是 onTextChanged ,反之亦然?例子将是最有启发性,注重为什么 onTextChanged 必须重写,但 afterTextChanged beforeTextChanged 不必被重写。

Under what circumstances should I use afterTextChanged instead of onTextChanged and vice versa? Examples would be most instructive with attention to why onTextChanged must be Overridden but afterTextChanged and beforeTextChanged do not have to be Overridden.

推荐答案

这些事件称为顺序如下:

These events are called in the following order:

  1. beforeTextChanged(CharSequence中,诠释开始,诠释计数,诠释后)
    这意味着字符即将替换一些新文本。的的文本编辑。
    使用:的时候,你需要看一看旧文本是即将改变
  2. onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数)
    已进行了更改,有些字刚刚被替换。的的文本编辑。
    使用:当你需要看这是在文本字符新
  3. afterTextChanged(编辑S)
    同上,除了现在的文字编辑的。照片 使用:的时候,你需要查看并可能修改新的文本
  1. beforeTextChanged(CharSequence s, int start, int count, int after).
    This means that the characters are about to be replaced with some new text. The text is uneditable.
    Use: when you need to take a look at the old text which is about to change.
  2. onTextChanged(CharSequence s, int start, int before, int count).
    Changes have been made, some characters have just been replaced. The text is uneditable.
    Use: when you need to see which characters in the text are new.
  3. afterTextChanged(Editable s).
    The same as above, except now the text is editable.
    Use: when a you need to see and possibly edit new text.

如果我只是听着的变化的EditText ,我不需要使用前两种方法都没有。如果需要,我将只接受在第三个方法和正确的新文本的新值。但是,如果我不得不追查其发生的确切数值的变化,我会用前两种方法。如果我也有一个需要听取修改后编辑的文字,我会做,在第三个方法。

If I'm just listening for changes in EditText, I won't need to use the first two methods at all. I will just receive new values in the third method and correct new text if needed. However, if I had to track down exact changes which happen to the values, I would use the first two methods. If I also had a need to edit the text after listening to the changes, I would do that in the third method.

这篇关于Android的TextWatcher.afterTextChanged VS TextWatcher.onTextChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆