android - TextWatcher 中 afterTextChanged 中的 setText 使退格键不起作用
问题描述
我正在使用addTextChangedListener(myTextWatcher)
我editText
的一些货币格式。我的setText()
里面有。afterTextChanged
TextWatcher
public void afterTextChanged(Editable s) {
et.removeTextChangedListener(this);
(snipped)
et.setText(someText);
(snipped)
et.addTextChangedListener(this);
}
问题是即使按住退格键我也只能删除一个字符。我发现setText
在afterTextChanged
使退格键不起作用的情况下。我不知道确切的机制,但它似乎setText
推动了退格。
此外,这似乎不会在 Android 7.0 中发生,而是在 android 8.0 和 9.0 中发生。
先感谢您!
解决方案
使用它来了解何时按下退格键:
editText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
//You can identify which key pressed buy checking keyCode value with KeyEvent.KEYCODE_
if(keyCode == KeyEvent.KEYCODE_DEL) {
editText.setText(editText.getText().tostring()); // gets the current text and sets it again
}
return false;
}
});
你说它只工作一次。通过这样做,如果您按下它,它会给您再次设置文本的机会,然后您只能再次按下退格键。
推荐阅读
- react-native - 我们如何在基于类的组件中将导航 4 的切换导航器替换为导航 5(不使用 redux)?
- postgresql - 根据条件连接多行的列值
- azure - azure容器组中的多个容器部署
- perl - 如何通过数组哈希中的值获取哈希的键
- java - 如何异步运行@ParameterizedTest
- javascript - 将 html 页面中的所有价格从一种货币更改为另一种货币?
- mysql - 有没有办法在 MySQL CASE 语句中对案例进行分组?
- apache-spark - 如何使用 sqoop 仅将两个表导入配置单元
- visual-studio-code - libxkbcommon.so.0:安装 VSCode 更新后没有可用的版本信息
- java - Sonar 和 findsecbugs 使用 Spring Expression 时的潜在代码注入