有线颜色问题 Android EditText

huangapple go评论112阅读模式
英文:

Wired coloring issue Android EditText

问题

我需要在EditText中的用户输入中添加类似于代码的主题。我执行此操作的方式是将文本词法分析为标记,每个标记都有值和类型,相同类型具有相同的颜色。之后,我将这些标记加载到我的EditText中并对其进行着色,但是出现了一个奇怪的错误。

这是错误的示例:
一切都在开始时正常着色有线颜色问题 Android EditText

在此之后发生:

有线颜色问题 Android EditText

这很奇怪,因为我的词法分析器工作正常,这是控制台输出:

    ColoredToken{key='KEYWORD', value='int'}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='NAME', value='a'}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='SIGNS', value='='}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='BID', value='10'}
    ColoredToken{key='KEYWORD', value='if'}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='SIGNS', value='('}
    ColoredToken{key='SIGNS', value=')'}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='SIGNS', value='{'}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='KEYWORD', value='int'}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='NAME', value='b'}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='SIGNS', value='='}
    ColoredToken{key='SPACE', value=' '}
    ColoredToken{key='BID', value='10'}
    ColoredToken{key='SIGNS', value='}'}

现在为了让你理解,蓝色用于名称,如第一张图片,关键字和符号为白色,问题是在键入一段时间后,一切开始变为蓝色,尽管标记是正确的。

这是着色和加载标记的代码:

    public static SpannableStringBuilder runCodeColor(String codeString) {
        ArrayList<String> code = new ArrayList<>(Arrays.asList(codeString.split("\n")));
        SpannableStringBuilder coloredCode = new SpannableStringBuilder("");
        int newLines = countNewLine(codeString);

        for (int i = 0; i < code.size(); i++) {
            ArrayList<ColoredToken> tmpTokens = new ArrayList<>(ColoredLexer.lexer(code.get(i)));
            for (int j = 0; j < tmpTokens.size(); j++) {
                coloredCode.append(tmpTokens.get(j).getValue());
                coloredCode.setSpan(tmpTokens.get(j).getDarkColorSpan(), coloredCode.length() - tmpTokens.get(j).getValue().length(), coloredCode.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                System.out.println(tmpTokens.get(j).toString());
            }
            if (newLines > 0) {
                coloredCode.append("\n");
                newLines--;
            }
        }
        return coloredCode;
    }

如果需要,我可以提供词法分析器代码,但如上所述,标记是正确的,问题在于着色。

编辑:
如果有人知道更好的代码着色/主题方式,如果您能提供一些示例代码并解释其工作原理,那将是很棒的。

英文:

I need to add code-like theme to the user input in EditText. The way I do it is by lexing text into tokens and each token has value and type, the same type has the same color. After that, I load those tokens into my EditText and color them, however, a wired bug happens.

This is an example of the bug:
Everything is coloring ok at the begining有线颜色问题 Android EditText

This happens after:

有线颜色问题 Android EditText

This is weird because my lexer works fine, this is the console output:

    ColoredToken{key=&#39;KEYWORD&#39;, value=&#39;int&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;NAME&#39;, value=&#39;a&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;SIGNS&#39;, value=&#39;=&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;BID&#39;, value=&#39;10&#39;}
    ColoredToken{key=&#39;KEYWORD&#39;, value=&#39;if&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;SIGNS&#39;, value=&#39;(&#39;}
    ColoredToken{key=&#39;SIGNS&#39;, value=&#39;)&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;SIGNS&#39;, value=&#39;{&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;KEYWORD&#39;, value=&#39;int&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;NAME&#39;, value=&#39;b&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;SIGNS&#39;, value=&#39;=&#39;}
    ColoredToken{key=&#39;SPACE&#39;, value=&#39; &#39;}
    ColoredToken{key=&#39;BID&#39;, value=&#39;10&#39;}
    ColoredToken{key=&#39;SIGNS&#39;, value=&#39;}&#39;}

Now for you to understand, the blue color is for names as in the first picture, white for keywords and signs, problem is that after typing for a while everything starts turning in blue, while tokens being correct.

This is the code for coloring and loading tokens:

    public static SpannableStringBuilder runCodeColor(String codeString) {
        ArrayList&lt;String&gt; code = new ArrayList&lt;&gt;(Arrays.asList(codeString.split(&quot;\n&quot;)));
        SpannableStringBuilder coloredCode = new SpannableStringBuilder(&quot;&quot;);
        int newLines = countNewLine(codeString);

        for (int i = 0; i &lt; code.size(); i++) {
            ArrayList&lt;ColoredToken&gt; tmpTokens = new ArrayList&lt;&gt;(ColoredLexer.lexer(code.get(i)));
            for (int j = 0; j &lt; tmpTokens.size(); j++) {
                coloredCode.append(tmpTokens.get(j).getValue());
                coloredCode.setSpan(tmpTokens.get(j).getDarkColorSpan(), coloredCode.length() - tmpTokens.get(j).getValue().length(), coloredCode.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                System.out.println(tmpTokens.get(j).toString());
            }
            if (newLines &gt; 0) {
                coloredCode.append(&quot;\n&quot;);
                newLines--;
            }
        }
        return coloredCode;
    }

If needed I can provide lexer code, but as stated above the tokens are alright, coloring is what's problematic.

EDIT:
If someone knows better ways of coloring/theming the code it would be great if you could provide some sample code and an explanation of how it works.

答案1

得分: 1

尝试使用文本处理器,这样您就不必自己实现,甚至可以对其进行训练,以适应您自己的语言。

英文:

Try to use textprocessor so that you do not have to implement this yourself, you can even train it for your own language

huangapple
  • 本文由 发表于 2020年10月27日 01:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64541818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定