1

您好朋友我正在使用以下代码以货币形式显示输入的金额。

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]", "");
                StringBuilder cashAmountBuilder = new StringBuilder(userInput);

                while (cashAmountBuilder.length() > 3 && cashAmountBuilder.charAt(0) == '0') {
                    cashAmountBuilder.deleteCharAt(0);
                }
                while (cashAmountBuilder.length() < 3) {
                    cashAmountBuilder.insert(0, '0');
                }
                cashAmountBuilder.insert(cashAmountBuilder.length()-2, '.');
                cashAmountBuilder.insert(0, '$');


                editAmount.setText(cashAmountBuilder.toString());
                editAmount.setTextKeepState(cashAmountBuilder.toString());
                Selection.setSelection(editAmount.getText(), cashAmountBuilder.toString().length());
            }
        }

该符号以“$”为前缀的问题我希望将其替换为新的 INR 符号或空白。我尝试用cashAmountBuilder.insert(0, '$');空白替换代码补丁,它给了我编译器错误。请同样帮助我。谢谢,

4

1 回答 1

0

伙计们找到了解决方案。希望与大家分享相同的内容。

editAmount=(EditText)findViewById(R.id.editTextPaymentAmount);
        mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
        editAmount.setRawInputType(Configuration.KEYBOARD_12KEY);
        editAmount.addTextChangedListener(new TextWatcher(){
                public void afterTextChanged(Editable s) {

                }
                public void beforeTextChanged(CharSequence s, int start, int count, int after){}
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(!s.toString().equals(current)){
                        editAmount.removeTextChangedListener((TextWatcher) this);
                //   String replaceable = String.format("[%s,.]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol()); 
                 //      String cleanString = s.toString().replaceAll(replaceable, "");
                   String cleanString = s.toString().replaceAll("[$,.]", "");
                 //  String cleanString = s.toString().  replaceAll("[^\\d]", "");
                       BigDecimal parsed = new BigDecimal(cleanString).setScale(2,BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100),BigDecimal.ROUND_FLOOR);                
                       String formato = NumberFormat.getCurrencyInstance().format(parsed);
    System.out.println(formato);
                       current = formato;
                       String newSt=formato.substring(1);

                       editAmount.setText(newSt);
                       editAmount.setSelection(newSt.length());

                       editAmount.addTextChangedListener((TextWatcher) this);
                    }
                }
            }); 
    }

public void priceClick(View view) {
        editAmount.addTextChangedListener(new TextWatcher(){
            DecimalFormat dec = new DecimalFormat("0.00");
            private String current = "";
            public void afterTextChanged(Editable arg0) {
            }

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

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(!s.toString().equals(current)){
                    editAmount.removeTextChangedListener((TextWatcher) this);

                   String cleanString = s.toString().replaceAll("[$,.]", "");

                   BigDecimal parsed = new BigDecimal(cleanString).setScale(2,BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100),BigDecimal.ROUND_FLOOR);                
                   String formato = NumberFormat.getCurrencyInstance().format(parsed);

                   current = formato;
                   editAmount.setText(formato);
                   editAmount.setSelection(formato.length());

                   editAmount.addTextChangedListener((TextWatcher) this);
                }
            }
        });
    }
于 2012-08-12T09:18:28.810 回答