久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

Android: Different colours for different characters in EditText(Android:EditText中不同字符的不同顏色)
本文介紹了Android:EditText中不同字符的不同顏色的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

提前感謝您的任何回復.

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中不同字符的不同顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 中文字幕二区 | 亚洲天堂一区 | 日韩免费高清视频 | 极品久久 | 国产精品久久久久久久久久免费看 | 亚洲日韩中文字幕一区 | 色综合色综合 | 99精品热视频| 久久国产精品偷 | 日韩久久久一区二区 | 97国产超碰 | 日韩一区二区三区视频 | 91av视频在线 | 成人精品视频在线观看 | 狠狠色香婷婷久久亚洲精品 | 国产高清精品在线 | 亚洲视频免费观看 | 一级毛片视频 | jav成人av免费播放 | 午夜男人免费视频 | 亚洲一区日韩 | 91免费观看国产 | 亚洲精品一区二区 | 欧美成人hd | 一区免费观看 | 欧美一区二区成人 | 欧美日韩毛片 | 成人免费视频网站在线观看 | 一级特黄色毛片 | www.久草.com| 久久久久国产一级毛片高清网站 | 久久精品成人热国产成 | 国产成人麻豆免费观看 | 亚洲精品一区二 | 中文天堂网 | 久久久这里都是精品 | 中文字幕在线观看国产 | 亚洲欧美视频 | 在线观看 亚洲 | 在线观看亚洲精品 | 欧美精品一区在线 |