简体   繁体   中英

Android EditText currency for Malaysia

 Locale currency = new Locale("ms-my");
                //Locale of Malaysia
                if (!editable.toString().equals(current)) {
                    TransferAmount.removeTextChangedListener(this);
                    String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance(currency)
                            .getCurrencyInstance().getCurrency().getSymbol());
                    String cleanString = editable.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;
                    TransferAmount.setSelection(formatted.length());
                    TransferAmount.addTextChangedListener(this);
                }

I would like to set the Currency as Malaysia and the Malaysia Country Code is "ms-my". I would like to add Malaysia currency symbol in front of the text. Is there anyway?

Simple solution would be add the Malaysia Country Code "ms-my" like a prefix

EditText editable = (EditText)findViewById(Your_id);
editable.addTextChangedListener(currencyFormatWatcher);

private final TextWatcher currencyFormatWatcher = new TextWatcher() {
    String prefix = "ms-my "; //"MYR "
    Locale currency = new Locale("ms-my");

    public void afterTextChanged(Editable s) {
        if(!s.toString().equals("")){
            editable.removeTextChangedListener(this);
            
            String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance(currency)
                        .getCurrencyInstance().getCurrency().getSymbol());
                String cleanString = editable.toString().replace(prefix, "").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));
                formatted = prefix.concat(formatted);
                
                            
            editable.setText(formatted);
            editable.setSelection(formatted.length());
            
            editable.addTextChangedListener(this);
        }
    }
};

Check this answers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM