且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android尝试替换字符并更改单词的颜色

更新时间:2023-02-23 08:09:48

我是通过以下方式解决的:

I solved it in this way:

if(nota != null){
        int firstIndex = nota.indexOf("*");
        if (firstIndex >= 0) {
            nota = nota.replaceFirst("[*]{1}", "");
            int secIndex = nota.indexOf("*");
            if (secIndex >= 0) {
                nota = nota.replaceFirst("[*]{1}", "");

                Spannable spannable = new SpannableString(nota);
                spannable.setSpan(new ForegroundColorSpan(Color.YELLOW), firstIndex, secIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannable.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), firstIndex, secIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                txtnota.setText(spannable);
            }
        }
}