gpt4 book ai didi

java - 当我完全删除该值时 onTextChanged

转载 作者:行者123 更新时间:2023-12-02 06:29:24 26 4
gpt4 key购买 nike

我正在尝试构建一个小费计算。这是我的代码的一部分:

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

double billAmount = Double.parseDouble(s.toString());
tip10EditText.setText("" + billAmount * .1);
tip15EditText.setText("" + billAmount * .15);
tip20EditText.setText("" + billAmount * .2);
total10EditText.setText("" + (billAmount + billAmount * 0.1));
total15EditText.setText("" + (billAmount + billAmount * 0.15));
total20EditText.setText("" + (billAmount + billAmount * 0.2));

}

问题是,当我删除账单编辑文本中的所有内容时,应用程序崩溃了。我的 friend 找到了一些方法来解决这个问题。这是他的代码:

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

if (!s.toString().equals("")) {
bill = Double.parseDouble(s.toString());

tip10EditText.setText((bill / 10) + "");
total10EditText.setText((bill + (bill / 10)) + "");

// the String.format("%.2f", is to SHORTEN the tail of the double to 2 digits
tip15EditText.setText(String.format("%.2f", (bill / 6.6666) ));
total15EditText.setText(String.format("%.2f",(bill + (bill / 6.6666)) ));
tip20EditText.setText(String.format("%.2f", (bill / 5) ));
total20EditText.setText(String.format("%.2f",(bill + (bill / 5)) ));

// to handle the seekBar changing according to the bill
double billCu = Double.parseDouble(billEditText.getText().toString());
// total20EditText.setText(String.format("%.2f",(tipOf ));
double progressPer=Double.parseDouble(customTipTextView.getText().toString());
double tipOf = (billCu/100)*progressPer;
tipCustomEditText.setText(String.format("%.2f",(tipOf )));
totalCustomEditText.setText(String.format("%.2f",((billCu+tipOf) )));
}
else {
tip10EditText.setText( "");
total10EditText.setText( "");
tip15EditText.setText( "");
total15EditText.setText( "");
tip20EditText.setText( "");
total20EditText.setText( "");
tipCustomEditText.setText("");
totalCustomEditText.setText("");
}
}

有没有更好的方法来处理这个问题?

最佳答案

public void onTextChanged(CharSequence s, int start, int before, int count) {
double billAmount = 0.0;
if(s.length() != 0){
billAmount = Double.parseDouble(s.toString());
}
tip10EditText.setText("" + billAmount * .1);
tip15EditText.setText("" + billAmount * .15);
tip20EditText.setText("" + billAmount * .2);
total10EditText.setText("" + (billAmount + billAmount * 0.1));
total15EditText.setText("" + (billAmount + billAmount * 0.15));
total20EditText.setText("" + (billAmount + billAmount * 0.2));
}

仅当文本框中存在任何输入时,上述代码才计算正确的值,否则显示 0。

关于java - 当我完全删除该值时 onTextChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20217457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com