车牌的TextWatcher

TextWatcher For Car Plate

我需要一个汽车牌照 "regex" (?) 例如:TextWatcher 的###-####(最多 8 位数字)。

事实上,我只需要将“-”放在字符数组的第 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。

我很感激任何提示

placaInput.addTextChangedListener(new TextWatcher() {

            int cursorPosition = 0;

            @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 editable) {

                placaInput.removeTextChangedListener(this);

                try {
                    cursorPosition = placaInput.getSelectionStart();
                    if (editable.length() > 0) {

                        String tempStr = "";
                        String newStr = "";
                        String str = placaInput.getText().toString();
                        String tempParamArr[] = str.split("-");
                        if (tempParamArr.length > 0) {
                            cursorPosition -= (tempParamArr.length - 1);
                            for (int i = 0; i < tempParamArr.length; i++) {
                                tempStr += tempParamArr[i];
                            }
                        } else {
                            tempStr = str;
                        }

                        for (int count = 0; count < tempStr.length(); count++) {
                            if (count == 3) {
                                newStr += "-";
                                newStr += tempStr.charAt(count);
                                cursorPosition++;
                            } else {
                                newStr += tempStr.charAt(count);
                            }
                        }
                        placaInput.setText(newStr);
                        if (newStr.length() > cursorPosition)
                            placaInput.setSelection(cursorPosition);
                        else
                            placaInput.setSelection(newStr.length());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                placaInput.addTextChangedListener(this);

            }
        });