1

我有这个问题,我如何在编辑文本上键入时格式化显示?例如,如果我输入 5,它将格式化为所需的货币。我可以在 onFocusChangeListener 上毫无问题地格式化 edittext 中的文本,但客户端需要 onchange。

这里有一些代码:

   <code>
    inputText = (EditText) findViewById(R.id.et_compare_rate_saving);
    inputText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)               {
            //System.out.println("beforeTextChanged::: => "+charSequence);
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //System.out.println("onTextChanged::: => "+charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(!editable.toString().isEmpty()){
                String amount = LO.removeSeparators(editable.toString(), getLocaleFormat());
                String newFormat = LO.compareRatesFormatting(Double.valueOf(amount),getLocaleFormat());
                System.out.println("afterTextChanged::: => "+newFormat);
                inputText.setText(newFormat);
            }
            //System.out.println("afterTextChanged::: => "+editable.toString());
        }
    });

   </code>

我有一个stackoverflow错误

  <code>
    08-01 22:37:32.724: ERROR/AndroidRuntime(1566): FATAL EXCEPTION: main
    java.lang.StackOverflowError
    at android.view.View.addFocusables(View.java:4311)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:731)
    at android.view.ViewGroup.addFocusables(ViewGroup.java:712)
    at android.view.View.getFocusables(View.java:4269)
    at android.view.FocusFinder.findNextFocus(FocusFinder.java:114)
    at android.view.FocusFinder.findNextFocus(FocusFinder.java:98)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:570)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.ViewGroup.focusSearch(ViewGroup.java:572)
    at android.view.View.focusSearch(View.java:4192)
    at android.widget.TextView.onCreateInputConnection(TextView.java:4936)
    at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:964)
    at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:919)

    </code>

任何想法?干杯。多谢。

4

2 回答 2

2

为了更清楚地说明这种情况,TextWatcher的文档明确指出在 afterTextChanged 中更改文本时应小心:

public abstract void afterTextChanged (Editable s)

调用此方法是为了通知您,在 s 中的某处,文本已更改。从这个回调中对 s 进行进一步的更改是合法的,但请注意不要让自己陷入无限循环,因为您所做的任何更改都会导致再次递归调用此方法。(您不会被告知更改发生的位置,因为其他 afterTextChanged() 方法可能已经进行了其他更改并使偏移量无效。但是如果您需要在这里知道,您可以在 onTextChanged 中使用 setSpan(Object, int, int, int) (CharSequence, int, int, int) 标记您的位置,然后从此处查找跨度结束的位置。

于 2012-08-01T15:46:14.277 回答
0

你处于一个无限循环中,这就是你得到stackoverflow的原因。每次文本更改时,都会调用您的 afterTextChanged,这会更改文本并导致再次调用它

于 2012-08-01T15:05:45.417 回答