Android TextWatcher.afterTextChanged 与 TextWatcher.onTextChanged

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/476848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:12:07  来源:igfitidea点击:

Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

androidandroid-textwatcher

提问by Will

Under what circumstances should I use afterTextChangedinstead of onTextChangedand vice versa?

在什么情况下我应该使用afterTextChanged代替,onTextChanged反之亦然?

回答by Malcolm

These events are called in the following order:

这些事件按以下顺序调用:

  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 you need to see and possibly edit the new text.

  1. beforeTextChanged(CharSequence s, int start, int count, int after)
    这意味着字符将被一些新文本替换。文本不可编辑。
    使用:当您需要查看即将更改的旧文本时。

  2. onTextChanged(CharSequence s, int start, int before, int count)
    进行了更改,刚刚替换了一些字符。文本不可编辑。
    使用:当您需要查看文本中哪些字符是新字符时。

  3. afterTextChanged(Editable s)
    同上,除了现在文本是可编辑的
    使用:当您需要查看并可能编辑新文本时。

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.

如果我只是在监听 中的变化EditText,我根本不需要使用前两种方法。我将在第三种方法中接收新值并在需要时更正新文本。但是,如果我必须追踪值发生的确切变化,我会使用前两种方法。如果我在听完更改后还需要编辑文本,我会在第三种方法中这样做。

回答by GregD

public void afterTextChanged(Editable s)

public void afterTextChanged(Editable s)

This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to sfrom 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 setSpan(Object, int, int, int)in onTextChanged(CharSequence, int, int, int)to mark your place and then look up from here where the span ended up.

调用此方法是为了通知您,在 内s的某处,文本已更改。s对此回调进行进一步更改是合法的,但请注意不要让自己陷入无限循环,因为您所做的任何更改都会导致再次递归调用此方法。(您不会被告知更改发生的位置,因为其他 afterTextChanged() 方法可能已经进行了其他更改并使偏移量无效。但是如果您需要在这里知道,您可以使用setSpan(Object, int, int, int)inonTextChanged(CharSequence, int, int, int)标记您的位置,然后从这里查找跨度结束了。

public void beforeTextChanged(CharSequence s, int start, int count, int after)

public void beforeTextChanged(CharSequence s, int start, int count, int after)

This method is called to notify you that, within s, the countcharacters beginning at startare about to be replaced by new text with length after. It is an error to attempt to make changes to sfrom this callback.

调用此方法是为了通知您,在 中s,以count开头的字符start即将被长度为 的新文本替换after。尝试s从此回调中进行更改是错误的。

public void onTextChanged(CharSequence s, int start, int before, int count)

public void onTextChanged(CharSequence s, int start, int before, int count)

This method is called to notify you that, within s, the countcharacters beginning at starthave just replaced old text that had length before. It is an error to attempt to make changes to sfrom this callback.

调用此方法是为了通知您,在 中s,以count开头的字符start刚刚替换了具有 length 的旧文本before。尝试s从此回调中进行更改是错误的。

Right from Android's Reference for TextWatcher.

直接来自Android 的 TextWatcher 参考

回答by IntelliJ Amiya

Android Textwatcheris one kind of trigger which is called on text change of an input field.

Android Textwatcher是一种触发器,在输入字段的文本更改时调用。

afterTextChanged (Editable s)- This method is called when the text has been changed. Because any changes you make will cause this method to be called again recursively, you have to be watchful about performing operations here, otherwise it might lead to infinite loop.

afterTextChanged (Editable s)- 当文本已更改时调用此方法。因为您所做的任何更改都会导致该方法再次被递归调用,所以您必须注意这里执行的操作,否则可能会导致无限循环

onTextChanged (CharSequence s, int start, int before, int count)- 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.

onTextChanged (CharSequence s, int start, int before, int count)- 调用此方法是为了通知您,在 s 内,从 start 开始的 count 个字符刚刚替换了之前具有 length 的旧文本。尝试从此回调中更改 s 是错误的。