使用TextWatcher的EditText很慢,我没有看到文本中的栏

时间:2014-01-20 16:08:34

标签: android textwatcher

实际上在EditText中我实现了一个Listener TextWatcher,问题是,如果我在文本内部单击,条形图在通常的位置保持静止,即使我删除了一些字母..为什么? ..也许是因为在afterTextChanged中我改变了文字颜色?

代码:

private final TextWatcher  mTextEditorWatcher = new TextWatcher() {

                    @Override
                    public void afterTextChanged(Editable arg0) {
                            // TODO Auto-generated method stub

                            setTextColor(arg0);

                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count,
                                    int after) {
                            // TODO Auto-generated method stub

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before,
                                    int count) {
                            // TODO Auto-generated method stub


                    }


             };

    private CharSequence setTextColor(Editable editable) {
                editable.clearSpans();

                if (editable.length() == 0) {
                    return editable;
                }


                    color(Patterns.HTML_OPEN_TAGS, editable);
                    color(Patterns.HTML_CLOSE_TAGS, editable);
                    color(Patterns.HTML_ATTRS, editable);
                    color(Patterns.GENERAL_STRINGS, editable);
                    color(Patterns.XML_COMMENTS, editable);

  return editable;
            }

 private void color(Pattern pattern,
                               Editable editable) {
                int color = 0;
                if (pattern.equals(Patterns.HTML_OPEN_TAGS)
                        || pattern.equals(Patterns.HTML_CLOSE_TAGS)
                        || pattern.equals(Patterns.GENERAL_KEYWORDS)
                    //|| pattern.equals(CSS_STYLE_NAME)
                        ) {
                    color = Patterns.COLOR_KEYWORD;
                } else if (pattern.equals(Patterns.HTML_ATTRS)
                        || pattern.equals(Patterns.CSS_ATTRS)) {
                    color = Patterns.COLOR_ATTR;
                } else if (pattern.equals(Patterns.CSS_ATTR_VALUE)) {
                    color = Patterns.COLOR_ATTR_VALUE;
                } else if (pattern.equals(Patterns.XML_COMMENTS)
                        || pattern.equals(Patterns.GENERAL_COMMENTS)) {
                    color = Patterns.COLOR_COMMENT;
                } else if (pattern.equals(
                        Patterns.GENERAL_STRINGS)) {
                    color = Patterns.COLOR_STRING;
                } else if (pattern.equals(Patterns.NUMBERS)) {
                    color = Patterns.COLOR_NUMBER;
                }

                for (final Matcher m =
                             pattern.matcher(editable);
                     m.find(); ) {
                    editable.setSpan(
                            new ForegroundColorSpan(color),
                            m.start(),
                            m.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }

1 个答案:

答案 0 :(得分:0)

  

调用setTextColor(arg0); on onTextChanged