Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged

徘徊边缘 提交于 2019-11-26 03:33:00

问题


Under what circumstances should I use afterTextChanged instead of onTextChanged and vice versa?


回答1:


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.

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.




回答2:


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 s 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 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.

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

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

public void 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.

Right from Android's Reference for TextWatcher.




回答3:


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

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.

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.



来源:https://stackoverflow.com/questions/476848/android-textwatcher-aftertextchanged-vs-textwatcher-ontextchanged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!