我正在实现一个场景,用户在编辑文本中输入一个数字答案,在使用addTextChangeListener输入文本时将其与两个数字相加。我正在尝试计算真假计数器。 我在下面的代码中完美地得到了真正的反击。
etTestAddAnswer.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) {
if (etTestAddAnswer.getText().toString().length() == (String.valueOf((firstDigit + secondDigit)).length())) {
if (etTestAddAnswer.getText().toString().equalsIgnoreCase("")) {
etTestAddAnswer.setError("Left Blank");
YoYo.with(Techniques.Wobble)
.duration(750)
.playOn(tlTestAddAnswer);
} else {
if (Integer.parseInt(etTestAddAnswer.getText().toString()) == (firstDigit + secondDigit)) {
correctCounter += 1;
etTestAddAnswer.setText("");
Toast.makeText(AdditionTestActivity.this, "Correct", Toast.LENGTH_SHORT).show();
if (finalCounter < 10) {
initGame();
} else {
testAddTime.cancel();
//Toast.makeText(AdditionTestActivity.this, "True Answer :" + correctCounter + "\n" + "Wrong Attempts" + falseCounter + "\n" + "Time " + toolbarTimeTestAddition.getText().toString(), Toast.LENGTH_SHORT).show();
}
} else {
YoYo.with(Techniques.RubberBand)
.duration(750)
.playOn(tlTestAddAnswer);
etTestAddAnswer.requestFocus();
falseCounter += 1;
Log.d(TAG, "False Answers: " + falseCounter);
etTestAddAnswer.setError("False Answer, \n Try Again..!!");
}
}
} else if (etTestAddAnswer.getText().toString().length() > (String.valueOf((firstDigit + secondDigit)).length())) {
YoYo.with(Techniques.RubberBand)
.duration(750)
.playOn(tlTestAddAnswer);
etTestAddAnswer.requestFocus();
Log.d(TAG, "onTextChanged: " + falseCounter);
//falseCounter++;
etTestAddAnswer.setError("False Answer, \n Try Again..!!");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
但是假计数器不正确。就像我在第五个问题并给出我的第一个错误输入一样,虚假计数器日志是这样的:
1
2
3
4
5
它应该只增加一个。任何想法??