android edittext货币格式,不带美元符号

android edittext货币格式,不带美元符号,android,android-edittext,Android,Android Edittext,我尝试在我的edittext中创建货币格式。我搜索并编写了代码,可以在我的edittext中添加货币格式 transfer_maney.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Ove

我尝试在我的edittext中创建货币格式。我搜索并编写了代码,可以在我的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) {

        }
    })
现在我想在输入中禁用/隐藏$symbol。可以隐藏/删除此符号(我希望此格式不包含此符号) 如果有人知道答案,请帮助我
感谢您的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;
    }

}
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.util.Log;
导入android.widget.EditText;
导入java.text.DecimalFormat;
导入java.util.Locale;
公共类PayTextWatcher实现TextWatcher
{
公共静态最终字符串T=“PayTextWatcher”;
私人最终编辑文本;
受保护的整数最大长度=整数最大值;
私有字符串格式类型;
私有字符串current=“”;
private boolean insertingSelected=false;
私有布尔删除;
/**
*@param editText
*@param formatType字符串格式样式,如“%,.2f$”
*/
公共PayTextWatcher(EditText,字符串格式类型)
{
this.editText=editText;
this.formatType=formatType;
Log.e(T,“::PayTextWatcher:”+“formatType”+formatType);
}
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after)
{
Log.i(T),::beforeTextChanged:“+”字符序列“+s+”开始=“+start+”计数=“+count+”之后=”+
之后);
如果(0之后)
{
isDeleting=true;
}否则
{
isDeleting=假;
}
如果(!s.toString().等于(当前))
{
removeTextChangedListener(此);
字符串clean_text=s.toString().replaceAll(“[^\\d]”,“”);
editText.setText(干净的文本);
editText.addTextChangedListener(此);
}
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count)
{
Log.i(T),::onTextChanged:“+”CharSequence“+s+”start=“+start+”count=“+count+”before=”+
之前);
如果(开始==0&&before>=4)
{
insertingSelected=true;
}
}
@凌驾
公共同步的void-afterextchanged(可编辑的)
{
Log.i(T),::后文本更改:“+”可编辑“+s+”当前“+Current”;
如果(!s.toString().等于(当前))
{
removeTextChangedListener(此);
字符串数字=s.toString();
如果(插入选定项)
{
digits=字符串.valueOf(toDouble(digits));
}
字符串格式的文本;
双v_值=0;
尝试
{
formatted_text=String.format(新语言环境(“pt”、“BR”)、formatType、Double.parseDouble(数字));
}捕获(NumberFormatException nfe)
{
v_值=toDouble(数字);
formatted_text=String.format(新语言环境(“pt”、“BR”)、FormattType、v_值);
}
当前=格式化的文本;
editText.setText(格式化文本);
editText.setSelection(格式化的_text.length());
editText.addTextChangedListener(此);
}
}
私有字符串deleteLastChar(字符串清除文本)
{
if(clean_text.length()>0)
{
clean_text=clean_text.substring(0,clean_text.length()-1);
}否则
{
清除文本=“0”;
}
返回干净的文本;
}
/**
*带有特殊字符的@param str字符串
*
*@返回字符串的双精度值
*/
公共双toDouble(字符串str)
{
str=str.replaceAll(“[^\\d]”,“”);
如果(str!=null&&str.length()>0)
{
double value=double.parseDouble(str);
字符串s_value=Double.toString(Math.abs(value/100));
int integerPlaces=s_值.indexOf('.');
if(整数位置>最大长度)
{
value=Double.parseDouble(deleteLastChar(str));
}
返回值/100;
}否则
{
返回0;
}
}
public int getMax_length()
{
返回最大长度;
}
公共无效设置最大长度(整数最大长度)
{
this.max_length=max_length;
}
}
结果是:


字符串StringObj=s.toString();如果(!StringObj.endsWith($)&&StringObj.contains($){
endwith?或startwith?@IntelliJAmiya-amiya如果您不想要货币符号,为什么要使用货币格式?只需使用十进制格式。这是我离完美作品越近的地方:(但是,当我输入第二个数字时,它仍然会进入文本的末尾。例如:当我输入5时,它会变成“5,00”。如果我再次输入5,它会变成“50,05”。删除后,它工作正常。@Leonardo您需要在textview上添加一个事件(onclick)若要在第一次触摸时选择所有文本,您将获得所需内容。如果您认为我应得,请向上投票;)它可以工作,但如果你在软键盘上按住backspace键,它不会删除文本。有什么建议吗?@cesarsicas我在这个类上做了很多更改,甚至更改了他的名字。今天它可以使用backspace键删除,自动生成最少的文本。我将链接到这里gitgist@ArthurMelo这个类有很多依赖项,但无论如何,我看不出是如何实现的你解决了退格删除的问题。有什么提示吗?