简体   繁体   English

EditText 无法将文本设置为格式化字符串

[英]EditText can't set text the formatted string

I want to format my EditText after every typing.我想在每次输入后格式化我的 EditText。 (eg: User types 1234 and I correct as 1.234,00) (例如:用户类型 1234,我更正为 1.234,00)

With this function, I format String correctly and I can see it on Toast message.有了这个 function,我可以正确格式化字符串,并且可以在 Toast 消息中看到它。 But whenever I try to set text to edittext, it gives error after typing second number.但是每当我尝试将文本设置为edittext时,输入第二个数字后都会出错。

This is my format function:这是我的格式 function:

 public String moneySeperator(double moneyAmount){
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setGroupingSeparator('.');
    symbols.setDecimalSeparator(',');

    DecimalFormat decimalFormat = new DecimalFormat("#,###.00", symbols);
    String prezzo = decimalFormat.format(moneyAmount);

    return prezzo;
}

Here is my EditText listener:这是我的 EditText 侦听器:

  editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        }

        @Override
        public void afterTextChanged(Editable editable) {
            editText.removeTextChangedListener(this);
            if(!editText.getText().toString().isEmpty()){

                String userInput = editText.getText().toString();

                Toast.makeText(Denemey.this, ""+moneySeperator(Double.parseDouble(userInput)), Toast.LENGTH_SHORT).show();
                editText.setText(""+moneySeperator(Double.parseDouble(userInput)));
            }
            editText.addTextChangedListener(this);
        }
    });

Here is the error: (It points editText.setText line)这是错误:(它指向editText.setText行)

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.incidijital.kdvtevkifati, PID: 7500
java.lang.NumberFormatException: For input string: "15,00"
    at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
    at java.lang.Double.parseDouble(Double.java:547)
    at com.myproject.dd$1.afterTextChanged(Denemey.java:46)

UPDATED: I made my code really shorter.更新:我的代码真的更短了。 When I try a double like 1234.50 it works well on printing, but I think there is something wrong about afterTextChanged listener.当我尝试像 1234.50 这样的双精度时,它在打印时效果很好,但我认为 afterTextChanged 监听器有问题。 My first 4 types (eg: 1,234) works well, after I add more it crash because of Double.parseDouble casting, but there is no comma.我的前 4 种类型(例如:1,234)运行良好,在我添加更多类型后,由于 Double.parseDouble 强制转换而崩溃,但没有逗号。 I removed formatter function.我删除了格式化程序 function。

@Override
        public void afterTextChanged(Editable editable) {


            editText.removeTextChangedListener(this);
            java.text.NumberFormat formatter = java.text.NumberFormat.getInstance(java.util.Locale.GERMANY);


            if(!editText.getText().toString().isEmpty()){
               double myDob = Double.parseDouble(""+editable);
                editText.setText(""+(formatter.format(myDob)));
                editText.setSelection(editText.getText().length());
            }

            editText.addTextChangedListener(this);
        }
    });

You have to remove all non numeric chars before parsing it as double.在将其解析为双精度之前,您必须删除所有非数字字符。 I am assuming here that your #moneySeperator working fine for format.我在这里假设您的#moneySeperator正常工作。

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void afterTextChanged(Editable editable) {
        editText.removeTextChangedListener(this);
        String userInput = editText.getText().toString().replaceAll("[^0-9]", "");
        if(!userInput.isEmpty()){
            Toast.makeText(Denemey.this, ""+moneySeperator(Double.parseDouble(userInput)), Toast.LENGTH_SHORT).show();
            editText.setText(""+moneySeperator(Double.parseDouble(userInput)));
        }else{
            editText.setText("")
        }
        editText.addTextChangedListener(this);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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