我有三个EditText元素。我想根据前两个edittext值进行计算,以存储在所需的编辑文本字段中。
但计算错误总是如此。每当我从secound编辑文本中清除键入的值时,计算就不能正常工作。
public void onTextChanged(CharSequence s, int a, int b, int c) {
try{
a=Integer.parseInt(totamt.getText().toString().trim());
b=Integer.parseInt(expenses.getText().toString().trim());
c=Integer.parseInt(handover_amt.getText().toString().trim());
int d = Integer.parseInt(String.valueOf(a)) - Integer.parseInt(String.valueOf(b)) - Integer.parseInt(String.valueOf(c));
balance_amt.setText(String.valueOf(d));
int myOB = Integer.parseInt(et_overall_balance.getText().toString().trim());
int OverallBalance = Integer.parseInt(String.valueOf(myOB)) - Integer.parseInt(handover_amt.getText().toString().trim());
et_overall_balance.setText(String.valueOf(OverallBalance));
System.out.println(OverallBalance);
}catch(NumberFormatException numberEx)
{
System.out.println(numberEx);
}
}
答案 0 :(得分:0)
使用以下代码
edittext2.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 (edittext1.getText().toString().trim() != null && !edittext1.getText().toString().trim().equalsIgnoreCase("")) {
if (edittext2.getText().toString().trim() != null && !edittext2.getText().toString().trim().equalsIgnoreCase("")) {
edittext3.setText("" + (Double.parseDouble(edittext1.getText().toString().trim()) - Double.parseDouble(edittext2.getText().toString().trim())));
} else {
Toast.makeText(activity, "Please enter value in edittext 2", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(activity, "Please enter value in edittext 1", Toast.LENGTH_LONG).show();
}
}
});