android - Edittext 移除 textchangedlistener
问题描述
我一直在寻找有关如何删除文本更改侦听器的答案。
这是我当前的代码:
public void enableTextChangedListener(boolean enableFormatting){
if (enableFormatting) {
if (!"1".equals(mAmountEditText.getTag())) {
mAmountEditText.addTextChangedListener(new StringUtils.NumberTextWatcherForThousand(mAmountEditText));
mAmountEditText.setTag("1");
}
}
else {
mAmountEditText.removeTextChangedListener(new StringUtils.NumberTextWatcherForThousand(mAmountEditText));
}
}
当我的布尔 enableFormatting 为 False 时,textchangelistener 仍然存在。
如果您想更清楚地解释我的代码,我可以提供 NumberTextwatcherForThousands 类。
解决方案
您需要保留对侦听器的引用,现在您在尝试删除旧侦听器时正在创建一个新侦听器。像这样的东西:
textWatcher = new StringUtils.NumberTextWatcherForThousand(mAmountEditText);
public void enableTextChangedListener(boolean enableFormatting){
if (enableFormatting) {
if (!"1".equals(mAmountEditText.getTag())) {
mAmountEditText.addTextChangedListener(textWatcher);
mAmountEditText.setTag("1");
}
}
else {
mAmountEditText.removeTextChangedListener(textWatcher);
}
}
推荐阅读
- x86-64 - AMD 架构上是否有与 Intel 的 MSR_SMI_COUNT 等效的寄存器?
- shell - 如何删除其中的管道符号和字符并修剪 ms 并以逗号分隔显示
- windows - 在powershell windows 8中搜索具有双引号的字符串
- c++ - 特征矩阵乘法比原始 SSE 慢
- jenkins - 如何在 Windows 机器上的詹金斯管道脚本中执行 shell 命令
- python - python代码获取每行文件的前两个字段
- javascript - 自定义元素执行顺序
- javascript - ERROR 错误:无法在 ViewContainer 中插入已破坏的视图
- python - Numpy矩阵列最小值索引
- swift - 如何在核心数据中执行复杂的获取请求