在 TextWatcher 中更改文本后 EditText 未更新

EditText not updated after text changed in the TextWatcher(在 TextWatcher 中更改文本后 EditText 未更新)

本文介绍了在 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 未更新

基础教程推荐