自动格式化的Java货币输入字段

问题描述 投票:0回答:1

想法是在保留货币格式的同时允许输入货币金额。例如:在空输入字段中输入1,然后输出应为$ 0.01。如果我再加上一个数字(例如0),则输出字符串应为$ 0.10。再加上一个数字(例如5),将得出$ 1.05。依此类推...

这里有一些示例代码,用于将货币格式添加到edittext字段的on文本更改侦听器。

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import java.text.DecimalFormat;
import java.text.ParseException;

//This class is for entering currency amount while keeping the right format as the user enters values
public class NumberTextWatcher implements TextWatcher {
    private final DecimalFormat dfnd;
    private final EditText et;

    public NumberTextWatcher(EditText editText) {
        dfnd = new DecimalFormat("#,##0.00");
        this.et = editText;
    }

    @Override
    public void afterTextChanged(Editable s) {
        et.removeTextChangedListener(this);
        //After all the text editing, if there is a string to validate - format it
        if (s != null && !s.toString().isEmpty()) {
            try {
                //Take the input string and remove all formatting characters
                String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "").replace("R","").replace(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()), "");
                //Pass the altered string to a number
                Number n = df.parse(v);
                //Get the decimal point correct again
                n = n.doubleValue() / 100.0;
                //Reformat the text with currency symbols, grouping places etc.
                et.setText(dfnd.format(n));
                //Add the Rand symbol
                et.setText("$".concat(et.getText().toString()));
                //Move the editing cursor back to the right place
                et.setSelection(et.getText().length());

            } catch (NumberFormatException | ParseException e) {
                e.printStackTrace();
            }
        } else //if the input field is empty
        {
            et.setText("$0.00");
        }

        et.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
    }
}

接下来,您只需将此新事件侦听器类链接到您的EditText输入字段

TextInputEditText mCashAmount = mView.findViewById(R.id.txtInput_CashAmount);
mCashAmount.addTextChangedListener(new NumberTextWatcher(mCashAmount));
java android currency sample
1个回答
-1
投票
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import java.text.DecimalFormat;
import java.text.ParseException;

//This class is for entering currency amount while keeping the right format as the user enters values
public class NumberTextWatcher implements TextWatcher {
    private final DecimalFormat dfnd;
    private final EditText et;

    public NumberTextWatcher(EditText editText) {
        dfnd = new DecimalFormat("#,##0.00");
        this.et = editText;
    }

    @Override
    public void afterTextChanged(Editable s) {
        et.removeTextChangedListener(this);
        //After all the text editing, if there is a string to validate - format it
        if (s != null && !s.toString().isEmpty()) {
            try {
                //Take the input string and remove all formatting characters
                String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "").replace("R","").replace(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()), "");
                //Pass the altered string to a number
                Number n = df.parse(v);
                //Get the decimal point correct again
                n = n.doubleValue() / 100.0;
                //Reformat the text with currency symbols, grouping places etc.
                et.setText(dfnd.format(n));
                //Add the Rand symbol
                et.setText("$".concat(et.getText().toString()));
                //Move the editing cursor back to the right place
                et.setSelection(et.getText().length());

            } catch (NumberFormatException | ParseException e) {
                e.printStackTrace();
            }
        } else //if the input field is empty
        {
            et.setText("$0.00");
        }

        et.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
    }
}

接下来,您只需将此新事件侦听器类链接到您的EditText输入字段

TextInputEditText mCashAmount = mView.findViewById(R.id.txtInput_CashAmount);
mCashAmount.addTextChangedListener(new NumberTextWatcher(mCashAmount));
© www.soinside.com 2019 - 2024. All rights reserved.