在文本更改中更改Android中的文本会导致溢出错误

Mic*_*Bak 8 android

我想创建一个应用程序,可以实时转换用户在EditText窗口小部件中输入的文本,并且我添加了一个TextWatcher允许我对文本更改执行操作的内容,但它导致溢出错误,因为我基本上创建了一个无穷无尽的loop(onTextChange -> code to change text -> onTextChange -> etc...).

任何人都知道如何解决这个问题?

这是一个例子

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");
}
Run Code Online (Sandbox Code Playgroud)

虽然以上实际上似乎正在起作用,但我无法使用它Html.fromHtml();,这是我打算使用的.

再次编辑

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!");
    }
}
Run Code Online (Sandbox Code Playgroud)

StackOverflowException在第37行抛出,这是" text.setText("Test!");"

Nik*_*ski 26

编辑:并且工作,没有抛出异常:

  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);
Run Code Online (Sandbox Code Playgroud)

这是Logcat的证明,当我大幅推送键时:

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
Run Code Online (Sandbox Code Playgroud)


bit*_*tle 6

private String originalValue;
void onTextChanged(CharSequence text, int start, int before, int after) {
   if (text.toString().equals(originalValue)) {
      // do nothing
   } else {
      //change your text
      originalValue = text.toString();
   }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

public void onTextChanged(CharSequence s, int start, int before, int count) {
       String currentText = s.toString();
       if (currentText.equals(originalValue)) { //the originalValue must be defined outside of the Watcher
          originalValue = styleText(currentText);
          // text.setText(originalValue); //if styleText doesn't do this
       } else {
          return;
       }
}
Run Code Online (Sandbox Code Playgroud)