我遇到这个问题,如何在edittext上格式化显示文件?例如,如果我键入5,它将格式化所需的货币。我可以在edittext中格式化文本而没有问题onFocusChangeListener但客户端需要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>
有什么想法吗? 干杯。 非常感谢。
答案 0 :(得分:2)
为了更清楚地说明这种情况,TextWatcher的文档明确指出在更改afterTextChanged中的文本时应该注意:
public abstract void afterTextChanged(Editable s)
调用此方法可以在某处通知您 在s内,文本已被更改。进一步发展是合理的 从此回调更改为s,但小心不要让自己 进入无限循环,因为你所做的任何改变都会导致这种情况 方法再次被递归调用。(你不知道在哪里 发生了更改,因为其他afterTextChanged()方法可能已经发生 进行了其他更改并使抵消无效。但如果你需要 要知道这里,你可以使用setSpan(Object,int,int,int) onTextChanged(CharSequence,int,int,int)标记您的位置然后 从这里向上看,跨度最终。
答案 1 :(得分:0)
你处于无限循环中,这就是你获得stackoverflow的原因。每次文本更改时,都会调用afterTextChanged,这会更改文本并使其再次被调用