Android - 对于粘贴文本中的每个字符调用一次 TextWatcher
我已将 TextWatcher 添加到 EditText 中,并通过 onTextChanged(CharSequence s, int start, int before, int count) 方法监听文本中的更改。当我将包含 10 个字符的文本粘贴到此 EditText 中时, onTextChanged() 被调用 10 次,从左到右,为我粘贴的文本中的每个字符调用一次。我希望 onTextChanged() 在所有 10 个字符都粘贴到 EditText 后仅调用一次。我确信这应该是可能的,因为否则如果“count”参数始终为 1,那么它还有什么意义呢?
I've added a TextWatcher to an EditText and am listening for changes in the text via the onTextChanged(CharSequence s, int start, int before, int count) method. When I paste text that has say 10 characters, into this EditText, onTextChanged() gets called 10 times, once for each character in the text I pasted, from left to right. I want onTextChanged() to be called only once after all 10 characters have been pasted into the EditText. I'm sure this should be possible, because otherwise what's the point in having the "count" param if it's always going to be 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
count
不会始终为 1:例如,如果您选择并删除一段文本或选择自动完成选项。无论如何,粘贴是一次发生在一个块中还是一次发生一个字符的细节是一个实现细节,如果您依赖于任何一种行为,那么您的应用程序将来可能会崩溃。
count
won't always be 1: for instance, if you select and delete a block of text or if you choose an autocomplete option.In any case, the details of whether pasting happens in one chunk or one character at a time is an implementation detail, and if you rely on either behavior it's likely your app will break in the future.
尝试使用 afterTextChanged 它只会接到一个电话
Try using afterTextChanged it will only get one call