【问题标题】:TextWatcher For Car Plate车牌TextWatcher
【发布时间】: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


    【解决方案1】:
    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);
    
                }
            });
    

    【讨论】:

    • 工作,但仍然有很多滞后,我正在使用材料设计
    猜你喜欢
    • 2019-12-28
    • 2017-07-05
    • 2016-12-28
    • 2011-06-11
    • 1970-01-01
    • 2012-07-13
    • 2012-07-22
    • 2012-07-29
    相关资源
    最近更新 更多