android edittext currency format without dollar sy

2019-04-10 21:01发布

问题:

I try to create currency format in my edittext.i searched and i wound code and i can add currency format in my edittext

        transfer_maney.addTextChangedListener(new TextWatcher() {
        @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().equals(current)) {
                transfer_maney.removeTextChangedListener(this);

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

                double parsed = Double.parseDouble(cleanString);
                String formatted = NumberFormat.getCurrencyInstance().format((parsed / 100));
                current = formatted;
                transfer_maney.setText(formatted);
                transfer_maney.setSelection(formatted.length());


            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    })

now i want to disable/hide $ symbol inside input.it is a possible to hide/delete this symbol(i want this format without this symbol) if anyone knows solution please help me thanks everyone

回答1:

Inside your Java code :

/* if you want $ inside editbox , put the $ before comma : "%$,.2f" */
final EditText valorTxt = (EditText) viewAccept.findViewById(R.id.valorInput);
final MoneyTextWatcher ptw = new MoneyTextWatcher(valorTxt, "%,.2f");
valorTxt.addTextChangedListener(ptw);

xml activity

<EditText
                    android:id="@+id/valorInput"
                    android:layout_width="0dp"
                    android:layout_weight="5"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:gravity="right"
                    android:hint="Value $"
                    android:inputType="numberDecimal"
                    android:textSize="20sp" />
                    </LinearLayout>

/*this class already accept copy past values */

public class PayTextWatcher implements TextWatcher

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

import java.text.DecimalFormat;
import java.util.Locale;

public class PayTextWatcher implements TextWatcher
{
    public static final String T = "PayTextWatcher";

    private final EditText editText;
    protected int max_length = Integer.MAX_VALUE;
    private String formatType;
    private String current = "";
    private boolean insertingSelected = false;
    private boolean isDeleting;

    /**
     * @param editText
     * @param formatType String formatting style like "%,.2f $"
     */
    public PayTextWatcher(EditText editText, String formatType)
    {
        this.editText = editText;
        this.formatType = formatType;
        Log.e(T, "::PayTextWatcher:" + "formatType " + formatType);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after)
    {
        Log.i(T, "::beforeTextChanged:" + "CharSequence " + s + " start=" + start + " count=" + count + " after=" +
                after);
        if (after <= 0 && count > 0)
        {
            isDeleting = true;
        } else
        {
            isDeleting = false;
        }
        if (!s.toString().equals(current))
        {
            editText.removeTextChangedListener(this);
            String clean_text = s.toString().replaceAll("[^\\d]", "");
            editText.setText(clean_text);
            editText.addTextChangedListener(this);
        }

    }


    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        Log.i(T, "::onTextChanged:" + "CharSequence " + s + " start=" + start + " count=" + count + " before=" +
                before);
        if (start == 0 && before >= 4)
        {
            insertingSelected = true;
        }
    }


    @Override
    public synchronized void afterTextChanged(Editable s)
    {
        Log.i(T, "::afterTextChanged:" + "Editable " + s + "; Current " + current);
        if (!s.toString().equals(current))
        {
            editText.removeTextChangedListener(this);
            String digits = s.toString();

            if (insertingSelected)
            {
                digits = String.valueOf(toDouble(digits));
            }
            String formatted_text;
            double v_value = 0;
            try
            {
                formatted_text = String.format(new Locale("pt", "BR"), formatType, Double.parseDouble(digits));

            } catch (NumberFormatException nfe)
            {
                v_value = toDouble(digits);
                formatted_text = String.format(new Locale("pt", "BR"), formatType, v_value);
            }

            current = formatted_text;
            editText.setText(formatted_text);
            editText.setSelection(formatted_text.length());
            editText.addTextChangedListener(this);
        }

    }

    private String deleteLastChar(String clean_text)
    {
        if (clean_text.length() > 0)
        {
            clean_text = clean_text.substring(0, clean_text.length() - 1);
        } else
        {
            clean_text = "0";
        }
        return clean_text;
    }

    /**
     * @param str String with special caracters
     *
     * @return a double value of string
     */
    public double toDouble(String str)
    {
        str = str.replaceAll("[^\\d]", "");
        if (str != null && str.length() > 0)
        {

            double value = Double.parseDouble(str);
            String s_value = Double.toString(Math.abs(value / 100));
            int integerPlaces = s_value.indexOf('.');
            if (integerPlaces > max_length)
            {
                value = Double.parseDouble(deleteLastChar(str));
            }

            return value / 100;
        } else
        {
            return 0;
        }
    }


    public int getMax_length()
    {
        return max_length;
    }


    public void setMax_length(int max_length)
    {
        this.max_length = max_length;
    }

}

The result is :