EditText not updated after text changed in the TextWatcher(在 TextWatcher 中更改文本后 EditText 未更新)
问题描述
我有一个 EditText 和一个 TextWatcher.
I have an EditText and a TextWatcher.
我的代码骨架:
EditText x;
x.addTextChangedListener(new XyzTextWatcher());
XyzTextWatcher implements TextWatcher() {
public synchronized void afterTextChanged(Editable text) {
formatText(text);
}
}
我的 formatText() 方法在文本的某些位置插入一些连字符.
My formatText() method inserts some hyphens at some positions of the text.
private void formatText(Editable text) {
removeSeparators(text);
if (text.length() >= 3) {
text.insert(3, "-");
}
if (text.length() >= 7) {
text.insert(7, "-");
}
}
private void removeSeparators(Editable text) {
int p = 0;
while (p < text.length()) {
if (text.charAt(p) == '-') {
text.delete(p, p + 1);
} else {
p++;
}
}
}
我遇到的问题是 - 我的 EditText 上显示的内容与 Editable 不同步.当我调试代码时,我看到变量 text (Editable) 具有预期值,但 EditText 上显示的内容并不总是与 Editable 匹配.
The problem I have is - what is displayed on my EditText isn't in sync with the Editable. When I debugged the code, I saw that the variable text (Editable) has the expected value, but what's shown on the EditText doesn't always match the Editable.
例如,当我有一个文本x = "123-456-789"我手动从 x 中剪切了文本456".格式化后,我的 Editable 的值为123-789-"但是,我的 EditText 上显示的值是123--789"
For example, when I have a text x = "123-456-789" I cut the text "456" from x manually. After formatting, my Editable has the value "123-789-" However, the value shown on my EditText is "123--789"
但在大多数情况下,它们具有相同的值.
They have the same value in most cases though.
我假设 EditText 是 Editable 并且它们应该始终匹配.我错过了什么吗?
I assumed that the EditText IS the Editable and they should always match. Am I missing something?
推荐答案
好吧,你从来没有真正改变 EditText 只是 Editable.Android EditTexts 不是 Editable 类的子级.字符串是 Editable 类的子类.onTextChangedListener 不接收 EditText 作为参数,而是接收 EditText 中显示的 Editable/String.使用连字符格式化 Editable 后,您需要更新 EditText.这样的事情应该可以正常工作:
Ok, you never actually change the EditText just the Editable. Android EditTexts are not children of the Editable class. Strings are subclasses of the Editable class. The onTextChangedListener doesn't receive the EditText as an argument but the Editable/String displayed in the EditText. After you format the Editable with the hyphens you then need to update the EditText. Something like this should work fine:
class MyClass extends Activity{
//I've ommited the onStart(), onPause(), onStop() etc.. methods
EditText x;
x.addTextChangedListener(new XyzTextWatcher());
XyzTextWatcher implements TextWatcher() {
public synchronized void afterTextChanged(Editable text) {
String s = formatText(text);
MyClass.this.x.setText(s);
}
}
}
为了防止速度变慢,为什么不改变 formatText 方法呢?
To prevent the slowdown why not change the formatText method something like this?
private Editable formatText(Editable text) {
int sep1Loc = 3;
int sep2Loc = 7;
if(text.length==sep1Loc)
text.append('-');
if(text.length==sep2Loc)
text.append('-');
return text;
}
注意:我没有测试过这个
这篇关于在 TextWatcher 中更改文本后 EditText 未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 TextWatcher 中更改文本后 EditText 未更新
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01