Android EditText AddTextChangeListener 货币格式

标签 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);
                }
            }
        });

最佳答案

根据这个 SO 问题 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);
    }

关于Android EditText AddTextChangeListener 货币格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27027070/

相关文章:

android - EditText 未显示在 Ice Cream Sandwich 下

android - 在 LinearLayout 中设置 EditText 宽度取决于 TextView 宽度

android - 使用 Vision API 自定义 RectangleDetector

android - Android 上的 PhoneGap : I have a bad information flow between HTML <-> JS <-> PlugIn <-> Activity

java - AsyncTask onPostExecute 在 doInBackground 之前调用

java - 如果在 Jetty 日志下禁用密码,JreDisabled :java. 安全意味着什么

android - 如何在 Android 11 中实现 New Storage API?

android - EditText 在键入时卡住/不显示文本

android - 如何在失去焦点后为 TextInputLayout 设置提示颜色

android - 第一次没有关注编辑文本