Android EditText AddTextChangeListener货币格式

时间:2014-11-19 21:12:05

标签: android android-edittext

通过SO,我将以下EditText addTextChangedListener事件处理程序放在一起,除了我需要的一些改进之外,它工作正常。当我输入时,它会从右到左填充数字。我宁愿它从左到右填充。现在,如果我输入50,格式为0.50美元,当我输入50时,我希望格式为$ 50.00

下面是xml声明和Java代码,请有人指出我需要更改以获得所需结果。

enter image description here

XML声明

    <EditText
        android:id="@+id/valueEditText"
        android:layout_row="1"
        android:hint="@string/hint_value_to_add"
        android:imeOptions="actionNext"
        android:inputType="numberDecimal"
        style="@style/EnrollEditViewStyle" />
    <requestFocus />

Java代码

      inputValue = (EditText) findViewById(R.id.valueEditText);
        inputValue.addTextChangedListener(new TextWatcher() {
            private String current = "";
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals(current)) {
                    inputValue.removeTextChangedListener(this);

                    String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
                    String cleanString = s.toString().replaceAll(replaceable, "");

                    double parsed;
                    try {
                        parsed = Double.parseDouble(cleanString);
                    } catch (NumberFormatException e) {
                        parsed = 0.00;
                    }
                    String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

                    current = formatted;
                    inputValue.setText(formatted);
                    inputValue.setSelection(formatted.length());
                    inputValue.addTextChangedListener(this);
                }
            }
        });

2 个答案:

答案 0 :(得分:4)

根据这个问题Format EditText to currency with whole numbers我将setMaximumFractionDigits添加到0就像这样

@Override
    public void afterTextChanged(Editable s) {
        if (!s.toString().equals(current)) {
            inputValue.removeTextChangedListener(this);

        String replaceable = String.format("[%s,.\\s]",   NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
        String cleanString = s.toString().replaceAll(replaceable, "");

        double parsed;
        try {
            parsed = Double.parseDouble(cleanString);
        } catch (NumberFormatException e) {
            parsed = 0.00;
        }
        NumberFormat formatter = NumberFormat.getCurrencyInstance();
        formatter.setMaximumFractionDigits(0);
        String formatted = formatter.format((parsed));

        current = formatted;
        inputValue.setText(formatted);
        inputValue.setSelection(formatted.length());
        inputValue.addTextChangedListener(this);
    }

答案 1 :(得分:2)

科林版本

var current = ""

editText.addTextChangedListener(object: TextWatcher {
    override fun afterTextChanged(s: Editable?) {}
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        val stringText = s.toString()

        if(stringText != current) {
            editText.removeTextChangedListener(this)

            val locale: Locale = Locale.UK
            val currency = Currency.getInstance(locale)
            val cleanString = stringText.replace("[${currency.symbol},.]".toRegex(), "")
            val parsed = cleanString.toDouble()
            val formatted = NumberFormat.getCurrencyInstance(locale).format(parsed / 100)

            current = formatted
            editText.setText(formatted)
            editText.setSelection(formatted.length)
            editText.addTextChangedListener(this)
        }
    }
})