我创建了一个包含一个 Ediitext 金额字段的回收器视图适配器,我在该 edittext 上应用了 textwatcher 以将值保存在当前对象中,但我想根据某些条件更改金额字段的值,例如,如果我输入值 100范围是 0 到 50,然后它会自动保存为零或给出一些错误消息
以下是我使用过的代码部分
this.edtAmount.addTextChangedListener(amountTextWatcher);
private class AmountTextWatcher implements TextWatcher {
private int position;
public void updatePosition(int position) {
this.position = position;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// no op
Log.d("Entered Val", "Values" + charSequence.toString());
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
Log.d("Entered Val", "Values" + charSequence.toString());
String amountStr=charSequence.toString().trim();
String pdcAmount=collections.get(position).getPdcAmt();
String balanceDue=collections.get(position).getAmountdue();
if(Constants.checkString(amountStr)){
collections.get(position).setAmountpaid(0);
return;
}
double amount=Constants.getRoundAmountDouble(decimalplace,amountStr);
double balance=Constants.getRoundAmountDouble(decimalplace,balanceDue);
if(!Constants.checkString(pdcAmount)){
balance= balance-Constants.getRoundAmountDouble(decimalplace,pdcAmount);
}
if(balance==0){
collections.get(position).setAmountpaid(0);
return;
}else if(balance<0 && (amount>0 || amount<balance)){
collections.get(position).setAmountpaid(0);
return;
}else if(balance>0){
if(amount<0){
collections.get(position).setAmountpaid(0);
return;
}else if(amount>balance){
collections.get(position).setAmountpaid(0);
return;
}
}
collections.get(position).setAmountpaid(amount);
if(!onbind)
notifyItemChanged(position);
}
@Override
public void afterTextChanged(Editable charSequence) {
}
}
但是我输入的每个字符edittext都失去了焦点
请指教
谢谢