TextWatcher events are being fired multiple times

后端 未结 7 2106
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 12:57

I have an annoying problem with TextWatcher. i\'ve been searching the web but couldnt find anything. appreciate if someone could assist me.

For some reason t

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 13:40

    According to the developer pages for TextWatcher, if a change is made to the Editable within TextWatcher, it will trigger further calls to all the TextWatchers linked to that Editable. Now, clearly your code doesn't trigger this behaviour.

    However, it is quite possible that if, for whatever reason, the system has a TextWatcher on the Editable, the situation you describe can occur. "Why", I hear you cry, "should this happen?"

    First, the classic defence: there is no reason for it not to happen and, strictly, app code should be written to be resilient to it.

    Second, I can't prove it, but I could well imagine that the code which handles layout of the displayed text within an EditText uses a TextWatcher to handle updating the display of the text on the screen. This code might insert control codes (which you aren't shown) into the Editable to ensure good line breaks and so on. It may even go round a loop a few times to get it right, and you might only get your first call after it has done all of its ...

    EDIT

    As per the comment by @Learn OpenGL ES, calling of a TextWatcher would be normal for things like autocorrect.

提交回复
热议问题