我在editText中实现自动更改格式编号时遇到问题。例如:我在editText中用“12.000”设置数字,但是当我在输入中添加数字时(我想要120.000),我得到错误或强制关闭。这是我的代码
EditText confirm_vol_before= (EditText)findViewById(R.id.confirm_vol_before);
//set value
confirm_vol_before.setText("12.000");
confirm_vol_before.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String val_confirm_vol_before = confirm_vol_before.getText().toString();
confirm_vol_before.setText(formatNumber(val_confirm_vol_before));
}
});
String formatNumber(String bd){
bd=bd.replace(".", "");
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
DecimalFormat formatter = new DecimalFormat("###,###.##", symbols);
bd=formatter.format(Long.parseLong(bd));
return bd;
}
和这个logcat
10-20 12:52:04.586: E/AndroidRuntime(12422): FATAL EXCEPTION: main
10-20 12:52:04.586: E/AndroidRuntime(12422): java.lang.StackOverflowError
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1013)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:913)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.TextUtils.getChars(TextUtils.java:70)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.TextUtils.getChars(TextUtils.java:70)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.graphics.Paint.getTextRunAdvances(Paint.java:1855)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.graphics.Paint.getTextRunAdvances(Paint.java:1809)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.TextLine.handleText(TextLine.java:759)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.TextLine.handleRun(TextLine.java:955)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.TextLine.measureRun(TextLine.java:414)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.TextLine.measure(TextLine.java:293)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.TextLine.metrics(TextLine.java:267)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.Layout.getLineExtent(Layout.java:974)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.Layout.getLineStartPos(Layout.java:500)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.Layout.getHorizontal(Layout.java:837)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.Layout.getHorizontal(Layout.java:809)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.text.Layout.getPrimaryHorizontal(Layout.java:793)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.getFocusedRect(TextView.java:5097)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.FocusFinder.findNextFocus(FocusFinder.java:129)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.FocusFinder.findNextInputFocus(FocusFinder.java:102)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.FocusFinder.findNextFocus(FocusFinder.java:83)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.FocusFinder.findNextFocus(FocusFinder.java:65)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:644)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.ViewGroup.focusSearch(ViewGroup.java:646)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.View.focusSearch(View.java:6198)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.onCreateInputConnection(TextView.java:5651)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1078)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1032)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3626)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3564)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.EditText.setText(EditText.java:100)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3539)
10-20 12:52:04.586: E/AndroidRuntime(12422): at com.ad.tanker.ViewFillOrder$2.afterTextChanged(ViewFillOrder.java:425)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.sendAfterTextChanged(TextView.java:7369)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3713)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3564)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.EditText.setText(EditText.java:100)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3539)
10-20 12:52:04.586: E/AndroidRuntime(12422): at com.ad.tanker.ViewFillOrder$2.afterTextChanged(ViewFillOrder.java:425)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.sendAfterTextChanged(TextView.java:7369)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3713)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3564)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.EditText.setText(EditText.java:100)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3539)
10-20 12:52:04.586: E/AndroidRuntime(12422): at com.ad.tanker.ViewFillOrder$2.afterTextChanged(ViewFillOrder.java:425)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.sendAfterTextChanged(TextView.java:7369)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3713)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3564)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.EditText.setText(EditText.java:100)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3539)
10-20 12:52:04.586: E/AndroidRuntime(12422): at com.ad.tanker.ViewFillOrder$2.afterTextChanged(ViewFillOrder.java:425)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.sendAfterTextChanged(TextView.java:7369)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3713)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3564)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.EditText.setText(EditText.java:100)
10-20 12:52:04.586: E/AndroidRuntime(12422): at android.widget.TextView.setText(TextView.java:3539)
10-20 12:52:04.586: E/AndroidRuntime(12422): at com.ad.tanker.ViewFillOrder$2.afterTextChanged(ViewFillOrd
那怎么解决呢?谢谢..对不起我的英文
答案 0 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
String formatNumber(String bd){
Double number = Double.valueOf(bd.replace(".", ""));
DecimalFormat dec = new DecimalFormat("#,##0.00");
return dec.format(number);
}