android edittext货币格式没有美元符号

时间:2015-12-17 06:18:50

标签: android android-edittext

我尝试在我的edittext.i搜索和创建代码中创建货币格式,我可以在我的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) {

        }
    })

现在我想在input.it中禁用/隐藏$符号,可以隐藏/删除此符号(我希望此格式不带此符号) 如果有人知道解决方案,请帮助我 谢谢大家

1 个答案:

答案 0 :(得分:1)

在Java代码中:

/* 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活动

<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>

/ *此类已接受复制过去的值* /

公共类PayTextWatcher实现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;
    }

}

结果是:

enter image description here