简体   繁体   中英

Remove $ symbol in Android EditText

I'm using this code to have a currency format on Android EditText control. But how can I remove the $ symbol (or replace with another symbol) from the result?

public void afterTextChanged(Editable s) {
    if (!s.toString().equals(current)) {
        inputValue.removeTextChangedListener(this);

        String cleanString = s.toString().replaceAll("\\D", "");

        if (cleanString.length() > 0) {
            double parsed = Double.parseDouble(cleanString);
            NumberFormat formatter = NumberFormat.getCurrencyInstance();
            formatter.setMaximumFractionDigits(0);
            current = formatter.format(parsed);
        } else {
            current = cleanString;
        }


        inputValue.setText(current);
        inputValue.setSelection(current.length());
        inputValue.addTextChangedListener(this);
    }
}

Thanks!

Screenshot

Heyy Vu Duong, I've tried something like this. Hope this helps you

  @Override
    public void afterTextChanged(Editable s) {

        if (!s.toString().equals(current)) {
            inputValue.removeTextChangedListener(this);

            String cleanString = s.toString().replaceAll("\\D", "");

            if (cleanString.length() > 0) {
                double parsed = Double.parseDouble(cleanString);
                NumberFormat formatter = NumberFormat.getCurrencyInstance();
                formatter.setMaximumFractionDigits(0);
                current = formatter.format(parsed);
                sub = current.substring(1, current.length());

            } else {
                current = cleanString;
            }

            inputValue.setText(current);
            inputValue.setSelection(current.length());
            inputValue.addTextChangedListener(this);
            Log.e("Amount ", "::" + sub);
        }
    }

What I've done here is, just added a String sub , which holds the value that you've entered in EditText. But note that String stub only takes value from position 1 (ie excluding $). If you go through the code i've printed it in log. So you can use this sub value further.

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