Android的TextWatcher.afterTextChanged VS TextWatcher.onTextChangedTextWatcher、Android、afterTextChange

2023-09-11 20:20:26 作者:你的时间允许不

在我应该用什么情况下 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:

beforeTextChanged(CharSequence中,诠释开始,诠释计数,诠释后)。 这意味着字符即将替换一些新文本。的的文本编辑。的 使用:的时候,你需要看一看旧文本是即将改变 onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数)。 已进行了更改,有些字刚刚被替换。的的文本编辑。的 使用:当你需要看这是在文本字符新的 afterTextChanged(编辑S)。 同上,除了现在的文字编辑的。照片 使用:的时候,你需要查看并可能修改新的文本 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. 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. 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.