Android:EditText中不同字符的不同颜色

Android: Different colours for different characters in EditText(Android:EditText中不同字符的不同颜色)
本文介绍了Android:EditText中不同字符的不同颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

提前感谢您的任何回复.

Thank you in advance for any responses.

我正在尝试在我的 Android 应用程序中添加一个 EditText,它对输入的不同字符具有不同的颜色.

I am trying to have an EditText in my Android application which has different colours for different characters which are typed in.

例如字母A"应始终为蓝色,字母b"应始终为绿色……以此类推.

For e.g. Alphabet "A" should always be blue, alphabet "b" should always be green... so on.

到目前为止,我还没有找到解决方案.请善待我,指引我正确的方向.

So far, I have no been able to find a solution for it. Please be kind enough to guide me in the right direction.

推荐答案

正如所指出的,您可以在输入文本时将 Spannables 应用于文本.像这样的:

As was pointed out, you can apply Spannables to the text as it is entered. Something like this:

colorEdit.addTextChangedListener(new TextWatcher() {

    String lastText = null;

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        Log.d("", "lastText='" + lastText + "'");
        Log.d("", "s='" + s + "'");
        if (!s.toString().equals(lastText)) {
            lastText = s.toString();

            String res = "";
            char[] split = s.toString().toCharArray();
            for (char c : split) {
                String color = null;
                if (c == 'a') {
                    color = "red";
                } else if (c == 'b') {
                    color = "green";
                } else if (c == 'c') {
                    color = "blue";
                }
                // etc...
                if (color != null) {
                    res += "<font color="" + color + "">" + c
                            + "</font>";
                } else {
                    res += c;
                }
            }
            int selectStart = colorEdit.getSelectionStart();
            int selectEnd = colorEdit.getSelectionEnd();
            colorEdit.setText(Html.fromHtml(res));
            colorEdit.setSelection(selectStart, selectEnd);
        }
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void afterTextChanged(Editable s) {}
});

一些需要注意的事情,我调用 setText 这当然会导致 onTextChanged 再次运行,所以我检查文本是否实际更改.此外,光标位置未正确保存,因此我也将其存储并恢复.

some things to note, I call setText which of course causes onTextChanged to run again, so I check that the text actually changed. Also, the cursor position was not saved correctly so I store and restore that as well.

这篇关于Android:EditText中不同字符的不同颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent enter key on EditText but still show the text as multi-line(防止在 EditText 上输入键,但仍将文本显示为多行)
Android keyboard next button issue on EditText(EditText上的Android键盘下一个按钮问题)
When the soft keyboard appears, it makes my EditText field lose focus(当软键盘出现时,它使我的 EditText 字段失去焦点)
android how to make text in an edittext exactly fixed lines(android如何在edittext中制作文本完全固定的行)
How to use regular expression in Android(如何在 Android 中使用正则表达式)
Filter list view from edit text(从编辑文本中过滤列表视图)