我有以下代码用于计算在 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)