TextWatcher For Car Plate - PullRequest
       14

TextWatcher For Car Plate

0 голосов
/ 23 марта 2020

Мне нужен номерной знак автомобиля "regex" (?), Например: ### - #### для TextWatcher (максимум 8 цифр).

На самом деле мне просто нужно поместите '-' на 4 место массива char (и обработайте клик на удаление) , но я застрял в нескольких циклах:

(я не знаю, как использовать old String) уверен, что это то, что приводит к l oop

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) {
        }
    });

Я нашел один полезный, но потребляет столько памяти и продолжает отставать пользовательский интерфейс.

Я ценю любой советы

1 Ответ

0 голосов
/ 23 марта 2020
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);

            }
        });
...