下面不是完整的代码,但这将给出实现的想法。
您必须创建一种新的计算和显示结果的方法 -
private void calculateAndShow(long wt, long rt, long mk){
double NetRate = rt + mk;
double Total = (NetRate / 10) * wt;
// Change the value here
barCode.setText(itembarcode);
itemDesc.setText(itemdesc);
weightLine.setText(weight);
rateAmount.setText(rate);
makingAmount.setText(making);
netRate.setText(NetRate + "");
itemtotal.setText(Total + "");
}
现在将这些行添加到您的方法之外 -
private TextWatcher rateTextWatcher = 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) {
String rate = rateAmount.getText().toString();
newRate = Double.parseDouble(rate);
calculateAndShow(wt, newRate, mk);
}
};
private TextWatcher makingAmountTextWatcher = 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) {
String makingAmount = makingAmount.getText().toString();
newMK = Double.parseDouble(makingAmount);
calculateAndShow(wt, rt, newMK);
}
};
这些文本观察器对您的编辑文本如下所示 -
EditText rateAmount = new EditText(AddInvEst.this);
rateAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
rateAmount.setGravity(Gravity.CENTER);
rateAmount.addTextChangedListener(rateTextWatcher);
EditText makingAmount = new EditText(AddInvEst.this);
makingAmount.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 0.5f));
makingAmount.setGravity(Gravity.CENTER);
makingAmount.addTextChangedListener(makingAmountTextWatcher);