覆盖onDraw时不显示EditText光标

时间:2014-05-21 10:55:38

标签: textview android

我已经在CustomEditText中覆盖onDraw以进行语法高亮显示,并尝试使用画布颜色而不是跨度(太慢)进行绘制。由于未调用super.onDraw(),因此光标不再可见。是否可以显示光标?

1 个答案:

答案 0 :(得分:1)

我的解决方案是使用默认渲染器绘制屏幕,​​然后使用适当的颜色进行透支。诀窍是调用super.onDraw mid方法。有关信息,我使用的是来自920-Text-Editor-master的libhighlight.so库。代码仍然需要工作,但我对结果感到满意。

   Highlight highlighter = new Highlight(); 

    @Override
    public void onDraw(final Canvas canvas)
    {
        int currentLine = getLayout().getLineForOffset(getSelectionStart());    
        int firstVisibleLineNumber = getLayout().getLineForVertical(getScrollY());
        int lastVisibleLineNumber = getLayout().getLineForVertical(getScrollY() + getHeight());
        int charWidth = (int) getPaint().measureText("8");

        for (int line = firstVisibleLineNumber; line < lastVisibleLineNumber; line++)
        {
            if (line == currentLine)
            {
                Rect r = new Rect();
                getLineBounds(line, r);
                canvas.drawRect(r, bgndPaint);
            }

            canvas.drawText(String.valueOf(line + 1),
                            5,
                            getLineBounds(line, null),
                            paintNumbers);
        }


        canvas.save();
        super.onDraw(canvas);
        canvas.restore();

        if (showSyntax == true)
        {
            int ret[] = highlighter.parse(getText().toString(), "/storage/emulated/0/projects/myIde/assets/html.conf");
            int spanQty = ret.length;
            if (spanQty < 1 || spanQty % 3.0F != 0)
            {
                return;
            }

            int styleQty = getText().toString().length();
            int[] style = new int[styleQty];
            for (int a = 0; a < styleQty;++a)
                style[a] = 0;

            for (int i = 0; i < spanQty; i++)
            {
                int group = ret[i];
                int start = ret[++i];
                int end   = ret[++i];

                int color = textColor;

                switch (group)
                {
                    case Highlight.GROUP_TAG_ID:
                        color = colorKeyword;
                        break;

                    case Highlight.GROUP_STRING_ID:
                        color = colorString;
                        break;

                    case Highlight.GROUP_KEYWORD_ID:
                        break;

                    case Highlight.GROUP_FUNCTION_ID:
                        break;

                    case Highlight.GROUP_COMMENT_ID:
                        color = colorComments;
                        break;

                    case Highlight.GROUP_ATTR_NAME_ID:
                        color = colorColor1;
                        break;

                    default:
                        continue;
                }

                for (int c = start; c < end; ++c)
                {
                    style[c] = color;
                }
            }


            for (int line = firstVisibleLineNumber; line < lastVisibleLineNumber; line++)
            {
                int start = getLayout().getLineStart(line);
                int end = getLayout().getLineEnd(line);

                String text = getText().toString().substring(start, end);

                for (int i = 0; i < text.length(); ++i)
                {
                    String c = text.substring(i, i + 1);

                    if (style[start + i] != 0)
                    {
                        getPaint().setColor(style[start + i]);
                        canvas.drawText(c, getPaddingLeft() + (i * charWidth), getLineBounds(line, null), getPaint());
                    }
                }
            }
        }
    }