1
爲了突出語法的目的,我已經在CustomEditText中覆蓋了onDraw,並嘗試使用畫布顏色而不是跨度(太慢)進行繪製。由於未調用super.onDraw(),所以遊標不再可見。是否可以顯示光標?覆蓋onDraw時不顯示EditText光標
爲了突出語法的目的,我已經在CustomEditText中覆蓋了onDraw,並嘗試使用畫布顏色而不是跨度(太慢)進行繪製。由於未調用super.onDraw(),所以遊標不再可見。是否可以顯示光標?覆蓋onDraw時不顯示EditText光標
我的解決方案是用默認的渲染器繪製屏幕,然後用適當的顏色重畫。訣竅是調用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());
}
}
}
}
}
莫非你過去你的代碼高亮顯示了你所面臨的問題的一個短版? – Grimmy