0

我有以下代码用于计算在 3 个 EditText 中的 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));

    }
}

问题是它在首先输入 dot(.) 而不是 0.x 时崩溃。我该如何防止这种情况?或者如果用户输入 .6 例如自动使其变为 0.6,我该如何制作?还有一种方法可以使 .(dot) 和 ,(comma) 都可以输入浮点值吗?在我的国家,我们通常使用逗号而不是点..

编辑: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)
4

2 回答 2

0

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

于 2013-06-25T21:30:12.443 回答
0

检查inputType的字段TextView。我认为如果您将其设置为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);
于 2013-06-25T20:44:06.540 回答