TextWatcher afterTextChanged导致计算器在android系统 [英] TextWatcher afterTextChanged causes stackoverflow in android

查看:417
本文介绍了TextWatcher afterTextChanged导致计算器在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法 drawItems()这每次创建一个新的布局,并将其设置为内容查看。我也有一个控制的EditText 这应该删除等元素时,它的内容被改变。

I have a method drawItems() which everytime creates a new layout and sets it as a contentView. And I also have a control EditText which should remove other elements when it's content is changed.

edit.addTextChangedListener(new TextWatcher() {

                    public void afterTextChanged(Editable s) {
                        currentText = s.toString();
                        drawItems();
                    }

                    public void beforeTextChanged(CharSequence s,
                            int start, int count, int after) {
                    }

                    public void onTextChanged(CharSequence s, int start,
                            int before, int count) {
                    }
                });

我想要做的是保存其当前文本,删除所有元素,只留下这个的EditText 保存字符串。当我试图运行该应用程序引发的错误是因为计算器它呈现的倍 drawItems 法无限多。为什么它呈现在 afterTextChanged drawItems即使我不改变它的内容?它的整个页面加载甚至在渲染。

What I want to do is save its current text, remove all elements and leave only this EditText with the saved string. When I'm trying to run this application the error raised is StackOverflow because it renders drawItems method infinite number of times. Why does it render drawItems within afterTextChanged even if I don't change its content? It's rendered even before the whole page is loaded.

推荐答案

此方法被调用来通知您,在小号的地方,文本已被更改。 这是合法的,从这个回调使送进一步的变化,但要小心不要让自己陷入死循环,因为你做将导致此方法的任何变化再次递归调用。(你是没有告诉那里的变化发生,因为其他afterTextChanged()方法可能已经做了修改和无效的偏移量。但是,如果你要了解的,您可以使用setSpan(对象,INT,INT,INT)在onTextChanged(CharSequence的, INT,INT,INT)来标记你的地方,然后从那里这里跨度结束了抬头。

This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.

放:

public void afterTextChanged(Editable s) {
    if (MyEditText.getText().toString().compareTo(s.toString()) != 0)
    {
        // your code ...
    }
}

这篇关于TextWatcher afterTextChanged导致计算器在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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