更改文本在安卓上的文字变化导致溢出错误 [英] Changing text in Android on text change causes overflow error

查看:151
本文介绍了更改文本在安卓上的文字变化导致溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以改变用户输入到一个的EditText 部件实时文本的应用程序,我添加了一个 TextWatcher 来让我做的东西上的文字变化,但它导致溢出错误,因为我基本上无限循环( onTextChange - > code键更改文本 - > onTextChange - >等等...

I want to create an application that can transform text the user enters in an EditText widget in real time, and I've added a TextWatcher to allow me to do stuff on text change, but it's causing an overflow error because I'm basically creating an endless loop (onTextChange -> code to change text -> onTextChange -> etc...).

任何人有关于如何解决这个问题的想法?

Anyone got an idea on how to get around this issue?

下面是一个例子

private boolean isEditable = true;
private EditText text;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    text = (EditText) findViewById(R.id.editText1);
    text.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

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

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (isEditable) {
                isEditable = false;
                styleText(s.toString());
            } else {
                isEditable = true;
            }
        }

    });

}

private void styleText(String completeText) {
    text.setText(completeText + " test");
}

虽然上面竟似乎是工作,我不能让它与 Html.fromHtml()工作; ,这正是我打算用

EDITED AGAIN

public class Main extends Activity implements TextWatcher {

    private EditText text;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        text = (EditText) findViewById(R.id.editText1);
        text.addTextChangedListener(this);
    }

    @Override
    public void afterTextChanged(Editable s) {
        text.removeTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        text.addTextChangedListener(this);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        text.removeTextChangedListener(this);
        text.setText("Test!");
    }
}

它扔了 StackOverflowException 在线37,这是 text.setText(测试!);

推荐答案

编辑:和作品,没有异常被抛出:

And works, no exception is thrown:

  txtwt = new TextWatcher(){

            @Override
            public void afterTextChanged(Editable s) {

            Log.i("REACHES AFTER", "YES");

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                Log.i("REACHES BEFORE", "YES");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                text.removeTextChangedListener(txtwt);//after this line you do the editing code 
                text.setText("TEST");
                Log.i("REACHES ON", "YES");
                text.addTextChangedListener(txtwt); // you register again for listener callbacks

            }};
       text = (EditText) findViewById(R.id.editText1);
        text.addTextChangedListener(txtwt);

这是logcat的证据,当我极大地推键:

This is the Logcat proof, when I dramatically pushed keys :

08-28 19:53:21.265: INFO/REACHES BEFORE(492): YES
08-28 19:53:21.276: INFO/REACHES ON(492): YES
08-28 19:53:21.276: INFO/REACHES AFTER(492): YES
08-28 19:53:21.296: INFO/REACHES BEFORE(492): YES
08-28 19:53:21.305: INFO/REACHES ON(492): YES
08-28 19:53:21.305: INFO/REACHES AFTER(492): YES
08-28 19:53:21.745: INFO/REACHES BEFORE(492): YES
08-28 19:53:21.755: INFO/REACHES ON(492): YES
08-28 19:53:21.755: INFO/REACHES AFTER(492): YES
08-28 19:53:21.775: INFO/REACHES BEFORE(492): YES
08-28 19:53:21.785: INFO/REACHES ON(492): YES
08-28 19:53:21.785: INFO/REACHES AFTER(492): YES
08-28 19:53:22.698: INFO/REACHES BEFORE(492): YES
08-28 19:53:22.705: INFO/REACHES ON(492): YES
08-28 19:53:22.705: INFO/REACHES AFTER(492): YES
08-28 19:53:23.855: INFO/REACHES BEFORE(492): YES
08-28 19:53:23.865: INFO/REACHES ON(492): YES
08-28 19:53:23.865: INFO/REACHES AFTER(492): YES
08-28 19:53:24.385: INFO/REACHES BEFORE(492): YES
08-28 19:53:24.395: INFO/REACHES ON(492): YES
08-28 19:53:24.395: INFO/REACHES AFTER(492): YES
08-28 19:53:24.485: INFO/REACHES BEFORE(492): YES
08-28 19:53:24.485: INFO/REACHES ON(492): YES
08-28 19:53:24.485: INFO/REACHES AFTER(492): YES
08-28 19:53:24.515: INFO/REACHES BEFORE(492): YES
08-28 19:53:24.525: INFO/REACHES ON(492): YES
08-28 19:53:24.525: INFO/REACHES AFTER(492): YES
08-28 19:53:24.625: INFO/REACHES BEFORE(492): YES
08-28 19:53:24.635: INFO/REACHES ON(492): YES
08-28 19:53:24.635: INFO/REACHES AFTER(492): YES
08-28 19:53:24.654: INFO/REACHES BEFORE(492): YES
08-28 19:53:24.665: INFO/REACHES ON(492): YES
08-28 19:53:24.665: INFO/REACHES AFTER(492): YES

这篇关于更改文本在安卓上的文字变化导致溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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