格式化货币输入editText的更好方法?

我有一个editText,起始值为$ 0.00。当您按数字1时,它将变为$ 0.01。按4,则变为$ 0.14。按8,$ 1.48。按退格键,$ 0.14等。


可行,问题是,如果有人手动定位光标,则格式化会出现问题。如果他们要删除小数,它将不会返回。如果他们将光标放在小数点前面并键入2,它将显示$ 02.00而不是$ 2.00。例如,如果他们尝试删除$,它将删除一个数字。


这是我正在使用的代码,我将不胜感激任何建议。


mEditPrice.setRawInputType(Configuration.KEYBOARD_12KEY);

    public void priceClick(View view) {

    mEditPrice.addTextChangedListener(new TextWatcher(){

        DecimalFormat dec = new DecimalFormat("0.00");

        @Override

        public void afterTextChanged(Editable arg0) {

        }

        @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(!s.toString().matches("^\\$(\\d{1,3}(\\,\\d{3})*|(\\d+))(\\.\\d{2})?$"))

            {

                String userInput= ""+s.toString().replaceAll("[^\\d]", "");

                if (userInput.length() > 0) {

                    Float in=Float.parseFloat(userInput);

                    float percen = in/100;

                    mEditPrice.setText("$"+dec.format(percen));

                    mEditPrice.setSelection(mEditPrice.getText().length());

                }

            }

        }

    });


慕容3067478
浏览 563回答 3
3回答

鸿蒙传说

我测试了您的方法,但是当我使用大量数字时它失败了……我创建了这个方法:private String current = "";@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {    if(!s.toString().equals(current)){       [your_edittext].removeTextChangedListener(this);       String cleanString = s.toString().replaceAll("[$,.]", "");       double parsed = Double.parseDouble(cleanString);       String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));       current = formatted;       [your_edittext].setText(formatted);       [your_edittext].setSelection(formatted.length());       [your_edittext].addTextChangedListener(this);    }}

qq_花开花谢_0

基于上述一些答案,我创建了一个MoneyTextWatcher,您可以按以下方式使用它:priceEditText.addTextChangedListener(new MoneyTextWatcher(priceEditText));这是课程:public class MoneyTextWatcher implements TextWatcher {&nbsp; &nbsp; private final WeakReference<EditText> editTextWeakReference;&nbsp; &nbsp; public MoneyTextWatcher(EditText editText) {&nbsp; &nbsp; &nbsp; &nbsp; editTextWeakReference = new WeakReference<EditText>(editText);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void beforeTextChanged(CharSequence s, int start, int count, int after) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTextChanged(CharSequence s, int start, int before, int count) {&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void afterTextChanged(Editable editable) {&nbsp; &nbsp; &nbsp; &nbsp; EditText editText = editTextWeakReference.get();&nbsp; &nbsp; &nbsp; &nbsp; if (editText == null) return;&nbsp; &nbsp; &nbsp; &nbsp; String s = editable.toString();&nbsp; &nbsp; &nbsp; &nbsp; if (s.isEmpty()) return;&nbsp; &nbsp; &nbsp; &nbsp; editText.removeTextChangedListener(this);&nbsp; &nbsp; &nbsp; &nbsp; String cleanString = s.replaceAll("[$,.]", "");&nbsp; &nbsp; &nbsp; &nbsp; BigDecimal parsed = new BigDecimal(cleanString).setScale(2, BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100), BigDecimal.ROUND_FLOOR);&nbsp; &nbsp; &nbsp; &nbsp; String formatted = NumberFormat.getCurrencyInstance().format(parsed);&nbsp; &nbsp; &nbsp; &nbsp; editText.setText(formatted);&nbsp; &nbsp; &nbsp; &nbsp; editText.setSelection(formatted.length());&nbsp; &nbsp; &nbsp; &nbsp; editText.addTextChangedListener(this);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android