我有一个计算,其中我必须从用户使用 EditText 给出的总数中减去 5 个变量,我的问题是:有没有办法排除任何值(et1、et2、et3、et4、et5)如果其中之一是 0?例如,如果用户将 500 作为“totaal”,对于 et1 & et2 为零,而对于其余四个,我只希望从“totaal”中减去 3*4
这是代码:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class Bereken extends AppCompatActivity {
private Double totaal = null;
TextView Txttotal;
Button k1;
Button k2;
Button k3;
TextView kies;
EditText ET1;
EditText ET2;
EditText ET3;
EditText ET4;
EditText ET5;
double temporary;
double randomdouble;
double formerval;
double totje;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bereken);
totaal = getIntent().getExtras().getDouble("Hannibal");
Txttotal = (TextView) findViewById(R.id.Tijdelijk);
Txttotal.setText(new DecimalFormat("##.##").format(totaal));
k1 = (Button)findViewById(R.id.optie1);
k2 = (Button)findViewById(R.id.optie2);
k3 = (Button)findViewById(R.id.optie3);
ET1 = (EditText)findViewById(R.id.editText7);
ET2 = (EditText)findViewById(R.id.editText8);
ET3 = (EditText)findViewById(R.id.editText9);
ET4 = (EditText)findViewById(R.id.editText10);
ET5 = (EditText)findViewById(R.id.editText11);
ET1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!ET1.getText().toString().equals("")) {
double et1 = Double.parseDouble(ET1.getText().toString());
double et2 = Double.parseDouble(ET2.getText().toString());
double et3 = Double.parseDouble(ET3.getText().toString());
double et4 = Double.parseDouble(ET4.getText().toString());
double et5 = Double.parseDouble(ET5.getText().toString());
totje = totaal - et1 - et2 - et3 - et4 - et5;
Txttotal.setText(new DecimalFormat("##.##").format(totje));
}else{
totaal = totaal + formerval;
Txttotal.setText(new DecimalFormat("##.##").format(totaal));
}
randomdouble = temporary;
}
});
ET2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
ET3.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
ET4.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
ET5.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
这就是错误
02-16 17:05:11.148 18568-18568/dipp.verlinden E/AndroidRuntime: FATAL EXCEPTION: main
Process: dipp.verlinden, PID: 18568
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at dipp.verlinden.Bereken$1.afterTextChanged(Bereken.java:64)
at android.widget.TextView.sendAfterTextChanged(TextView.java:9195)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:11939)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1047)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:564)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:494)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:34)
at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:691)
at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:199)
at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:184)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:286)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)