我有一个字符串名称"classe".它包含许多单词和短语.但是我希望在字符串包含单词时更改颜色"idiom".我以编程方式执行了此操作,但它更改了所有字符串"classe"的颜色.我只是想改变"成语"的颜色(单词的一部分,而不是整个短语).我该如何以编程方式执行此操作?
if (classe.contains("idiom")) {
txtclasse.setTextColor(getResources().getColor(R.color.orange));
}
Run Code Online (Sandbox Code Playgroud)
使用ForegroundColorSpan.
if(classe != null && classe.contains(“idiom”))
{
Spannable spannable = new SpannableString(classe);
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), classe.indexOf(“idiom”), classe.indexOf(“idiom”) + “idiom”.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtclasse.setText(spannable);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2723 次 |
| 最近记录: |