Android TextWatcher afterTextChanged

时间:2013-09-18 20:42:54

标签: android textwatcher

我必须取EditText的值并除以1.21 ....其中是错误

private TextWatcher filterTextWatcher = new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }
    public void onTextChanged(CharSequence s, int start, int before, int end) {


        }
    public void afterTextChanged(Editable s) {

        double imponibile21;
        Importo.getText().toString();
        if (spinnerIva.getSelectedItem().toString().equals("21%")){

        Double value = Double.valueOf(""+Importo);
        imponibile21 = (value/1.21);

        NumberFormat formatter = NumberFormat.getCurrencyInstance();
        String Imponibile = formatter.format(imponibile21 );
        mImponibile.setText(Imponibile);

错误:

    09-18 22:59:56.617: E/AndroidRuntime(27425): java.lang.NumberFormatException: Invalid double: "android.widget.EditText{421f9c38 VFED..CL .F...... 0,664-1080,781 #7f08000b app:id/ed_importo_fattura}"
09-18 22:59:56.617: E/AndroidRuntime(27425):    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at java.lang.StringToReal.initialParse(StringToReal.java:114)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at java.lang.StringToReal.parseDouble(StringToReal.java:263)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at java.lang.Double.parseDouble(Double.java:295)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at java.lang.Double.valueOf(Double.java:332)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at fatture.acquisti.Inserisci_fatture$2.afterTextChanged(Inserisci_fatture.java:178)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.widget.TextView.sendAfterTextChanged(TextView.java:7841)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9754)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:676)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:196)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.os.Looper.loop(Looper.java:137)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at android.app.ActivityThread.main(ActivityThread.java:5328)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at java.lang.reflect.Method.invokeNative(Native Method)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at java.lang.reflect.Method.invoke(Method.java:511)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-18 22:59:56.617: E/AndroidRuntime(27425):    at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:1)

您从StringDouble的转换无效,因此java.lang.StringToReal.invalidReal(StringToReal.java:63)错误。此错误发生在代码中的第63行(java:63)。

试试这个:

String stringValue = Importo.getText().toString();
if(!stringValue.isEmpty()) {
    Double value = Double.valueOf(stringValue);
}

答案 1 :(得分:0)

您似乎正在尝试将EditText解析为Double而不是包含该值的String

尝试类似的东西:

public void afterTextChanged(Editable s) {

    double imponibile21;
    String val = s.toString();
    if (spinnerIva.getSelectedItem().toString().equals("21%")){

        Double value = Double.valueOf(val);

确保val包含有效的double

答案 2 :(得分:0)

String number = Importo.getText().toString();
Double value = Double.valueOf(""+ number);

这就是你需要的。

答案 3 :(得分:0)

implements TextWatcher添加到您的班级。 这解决了我的问题。我导入TextWatcher但它没有用,然后我尝试实现它并且它完美地工作。