1

Hello Friends 次のコードを使用して、通貨の形で入力された金額を表示しています。

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 に答える