How can I change the EditText text without triggering the Text Watcher?(如何在不触发 Text Watcher 的情况下更改 EditText 文本?)
问题描述
我有一个带有 Customer Text Watcher 的 EditText
字段.在一段代码中,我需要使用 .setText("whatever")
.
I have an EditText
field with a Customer Text Watcher on it. In a piece of code I need to change the value in the EditText which I do using .setText("whatever")
.
问题是,一旦我进行了更改,就会调用 afterTextChanged
方法,这会创建一个无限循环.如何在不触发 afterTextChanged 的情况下更改文本?
The problem is as soon as I make that change the afterTextChanged
method gets called which created an infinite loop. How can I change the text without it triggering afterTextChanged?
我需要 afterTextChanged 方法中的文本,所以不建议删除 TextWatcher
.
I need the text in the afterTextChanged method so don't suggest removing the TextWatcher
.
推荐答案
你可以注销watcher,然后重新注册.
You could unregister the watcher, and then re-register it.
或者,您可以设置一个标志,以便您的观察者知道您何时自己更改了文本(因此应该忽略它).
Alternatively, you could set a flag so that your watcher knows when you have just changed the text yourself (and therefore should ignore it).
这篇关于如何在不触发 Text Watcher 的情况下更改 EditText 文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不触发 Text Watcher 的情况下更改 EditText 文本?
![](/xwassets/images/pre.png)
![](/xwassets/images/next.png)
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01