在文本视图中仅更改一个世界的颜色

Changing the color of only one world in a text view

我正在使用 android studio 创建一个 android 应用程序,它使用 volley 库从 mysqli 数据库检索文本到文本视图中。我想检查文本是否包含“@”,如果包含它,它应该是红色的。也就是说,我只想更改“@”。请问大家我将如何实现?

使用像

这样的 if 语句
if text.contains("@") {
text.setTextColor(Color.RED);
}

阅读此处 How to set the text color of TextView in code? 了解有关更改文本颜色的更多信息

在这种情况下,这只会改变一个单词“@”的颜色

这将更改单个字母的颜色,在本例中为“@”

String newText = String.valueOf(text.getText()).replace("@","<font color='#fff0000'>@</font>");
        text.setText(Html.fromHtml(newText));