【发布时间】:2020-03-23 10:11:05
【问题描述】:
我需要汽车牌照“正则表达式”(?)例如:###-#### 用于 TextWatcher(最多 8 位数字)。
事实上,我只需要将 '-' 放在 char 数组的 4 个位置(并处理删除点击),但我被困在几个循环中:
(我不知道如何使用 old 字符串)确定这是产生循环的原因
placaInput.addTextChangedListener(new TextWatcher() {
boolean isUpdating;
String old = "";
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str;
str = s.toString().toUpperCase();
if (str.contains("-")) {
str = str.replace("-", "");
}
if (isUpdating) {
old = str;
isUpdating = false;
return;
}
String newStr = "";
try {
for (int i = 0; i < str.length(); i++) {
if (str.length() < 3) {
newStr += str.charAt(i);
} else if (str.length() == 3) {
newStr = str + "-";
} else if(i > 3 && i <= 7){
newStr += str.charAt(i);
}
}
} catch (Exception e) {
e.printStackTrace();
}
isUpdating = true;
placaInput.setText(newStr);
placaInput.setSelection(newStr.length());
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
我发现了一个有用但消耗大量内存并一直滞后于 UI 的方法。
感谢任何提示
【问题讨论】:
标签: android performance textwatcher