更好的方式来格式化货币输入EDITTEXT?货币、方式、EDITTEXT

2023-09-12 00:31:36 作者:刚泡了个小哥哥,一不小心把他给烫死了。今日小编为小伙伴们送上

我有一个EditText,初值为$ 0.00元。当你preSS 1,它更改为$ 0.01 preSS 4,它进入$ 0.14 preSS 8,$ 1.48 preSS退格,$ 0.14等。

这是工作,但问题是,如果有人手动光标定位,发生在格式问题。如果他们删除小数,它不会再回来了。如果他们把光标在小数和类型2的前面,它会显示$ 02.00,而不是$ 2,00。如果他们试图删除$它将删除一个数字代替,例如

下面是code我用,我倒是AP preciate任何建议。

  mEditPrice.setRawInputType(Configuration.KEYBOARD_12KEY);
    公共无效priceClick(查看视图){
    mEditPrice.addTextChangedListener(新TextWatcher(){
        DecimalFormat的DEC =新的DecimalFormat(0.00);
        @覆盖
        公共无效afterTextChanged(编辑为arg0){
        }
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,
                诠释计数,之后INT){
        }
        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,
                INT之前,诠释计数){
            如果(s.toString()的比赛(^ \\ $(\\Ð{1,3}(\\ \\ð{3})* |!。(\\ D +))(\\ ð{2})?$))
            {
                串userInput =+ s.toString()的replaceAll。([^ \\ D]。,);
                如果(userInput.length()大于0){
                    漂浮在= Float.parseFloat(userInput);
                    浮percen = IN / 100;
                    mEditPrice.setText($+ dec.format(percen));
                    mEditPrice.setSelection(mEditPrice.getText()长度());
                }
            }
        }
    });
 
搜狗输入法V模式有什么功能

解决方案

我测试你的方法,但是当我使用大批失败......我创造了这个:

 私人串电流=;
@覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    如果(!s.toString()。等于(电流)){
       [your_edittext] .removeTextChangedListener(本);

       字符串cleanString = s.toString()的replaceAll([$ ...],)。

       双解析= Double.parseDouble(cleanString);
       。串格式化= NumberFormat.getCurrencyInstance()格式((解析/ 100));

       电流=格式;
       [your_edittext] .setText(格式化);
       [your_edittext] .setSelection(formatted.length());

       [your_edittext] .addTextChangedListener(本);
    }
}
 

I have an editText, starting value is $0.00. When you press 1, it changes to $0.01. Press 4, it goes to $0.14. Press 8, $1.48. Press backspace, $0.14, etc.

That works, the problem is, if somebody manually positions the cursor, problems occur in the formatting. If they were to delete the decimal, it won't come back. If they put the cursor in front of the decimal and type 2, it will display $02.00 instead of $2.00. If they try to delete the $ it will delete a digit instead, for example.

Here is code I'm using, I'd appreciate any suggestions.

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());
                }
            }
        }
    });

解决方案

I tested your method, but it fails when i use great numbers... i created this:

private String current = "";
@Override
public 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);
    }
}