输入点(。)计算器时崩溃

时间:2013-06-25 20:06:58

标签: android

我有以下代码来计算在3个EditTexts中的2个中输入的值。

public void calculeaza() {

    totaltest = 0;
    prod = new String[allprod.size()];
    pret = new String[allpret.size()];
    cant = new String[allcant.size()];

    for (int m = 0; m < allprod.size(); m++) {

        prod[m] = allprod.get(m).getText().toString();
        if (prod[m].matches("")) {
            prod[m] = Float.toString(0);

        }
    }

    for (int j = 0; j < allcant.size(); j++) {

        cant[j] = allcant.get(j).getText().toString();
        if (cant[j].matches("")) {
            cant[j] = Float.toString(0);

        }
    }

    for (int k = 0; k < allpret.size(); k++) {
        pret[k] = allpret.get(k).getText().toString();
        if (pret[k].matches("")) {
            pret[k] = Float.toString(0);

        }
    }

    for (int l = 0; l < allpret.size(); l++) {

        Float temp = Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]);

        totaltest = totaltest + temp;

        TextView totalf = (TextView) findViewById(R.id.total);
        totalf.setText(String.format("Total: %.2f", totaltest));

    }
}

问题是它首先输入点(。)而不是0.x时崩溃。我怎么能阻止这个?或者如何让用户输入.6例如自动使其为0.6? 还有一种方法可以为浮点值输入。(点)和(逗号)吗?在我的国家,我们通常使用逗号而不是点..

编辑:Logcat:调用calculeaza()

时失败
06-26 00:23:53.952: E/AndroidRuntime(15220): FATAL EXCEPTION: main
06-26 00:23:53.952: E/AndroidRuntime(15220): java.lang.NumberFormatException: Invalid float: ""
06-26 00:23:53.952: E/AndroidRuntime(15220):    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at java.lang.StringToReal.initialParse(StringToReal.java:163)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at java.lang.StringToReal.parseFloat(StringToReal.java:304)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at java.lang.Float.parseFloat(Float.java:300)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at com.example.testlayout.MainActivity.calculeaza(MainActivity.java:494)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at com.example.testlayout.MainActivity.onTextChanged(MainActivity.java:517)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.widget.TextView.sendOnTextChanged(TextView.java:7231)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.widget.TextView.handleTextChanged(TextView.java:7290)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:8880)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:672)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:196)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.os.Looper.loop(Looper.java:137)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at android.app.ActivityThread.main(ActivityThread.java:5041)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at java.lang.reflect.Method.invokeNative(Native Method)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at java.lang.reflect.Method.invoke(Method.java:511)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-26 00:23:53.952: E/AndroidRuntime(15220):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

结帐TextView的{​​{3}}字段。我想如果你把它设置为numberDecimal,它将始终采用正确的格式。

布局:

EditText android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />

代码:

EditText ed = (EditText) findViewById(R.id.textView);
String s = ed.getText().toString();
float f = Float.valueOf(s);

Log.e(TAG, "Value: " + f);

答案 1 :(得分:0)

在editText上使用TextWatcher查找“。” ,将你的字符串相应地转换为小数部分并将其转换为双精度点。你用最终结果计算。