EditText上没有更新的TextWatcher后的文本已改变 [英] EditText not updated after text changed in the TextWatcher

查看:117
本文介绍了EditText上没有更新的TextWatcher后的文本已改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText和TextWatcher。

I have an EditText and a TextWatcher.

我的code骨架:

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不与可编辑的同步。当我调试的code,我看到了可变文本(可编辑)的预期值,但什​​么是显示在EditText上并不总是一致的编辑。

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。 格式化后,我可编辑的值为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 IS的可编辑的,他们应该总是匹配。我失去了一些东西?

I assumed that the EditText IS the Editable and they should always match. Am I missing something?

推荐答案

好吧,你从来没有真正改变的EditText只是编辑。 Android的EditTexts不是编辑类的子类。字符串是可编辑类的子类。该onTextChangedListener没有收到的EditText作为参数,但可编辑/字符串显示在EditText上。在格式化后可编辑的连字符的然后需要更新的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);
        }
    }

}

要prevent放缓为什么不改变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;
}

注:我没有测试过这种

这篇关于EditText上没有更新的TextWatcher后的文本已改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆