【发布时间】:2017-06-08 19:34:29
【问题描述】:
我是在edittext中自动显示4位连字符后实现的(2015-07)。我的代码工作正常,但问题是我在 4 位数字值之前删除并再次输入它不起作用。当我像 2015-07 到 2014-07 一样重新输入 edidtext 时,addTextChangedListener 不会触发。但是当我使用“/”而不是“-”时,我可以重新输入值。有什么问题?
mEdtProductionCode.addTextChangedListener(new TextWatcher() {
int prevL = 0;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
prevL = mEdtProductionCode.getText().toString().length();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int length = s.length();
if ((prevL < length) && length == 4) {
String data = mEdtProductionCode.getText().toString();
mEdtProductionCode.setText(data + "-");
mEdtProductionCode.setSelection(length + 1);
}
}
});
【问题讨论】:
-
没人回答??
-
问题在于
prevL与length比较。如果您删除第 4 位之前的字符,您的prevL将大于length并且不会执行afterTextChanged方法中的代码
标签: android