如何在Android中为字符串设置颜色

时间:2019-03-27 08:17:57

标签: android string textview

我有一个webview。我只是在webview's中检索string内容,并在textview中显示它。在onTouch事件中,文本将大声朗读。现在,我想在onTouch事件中逐字突出显示文本。对于textview,我们可以使用setTextColor。但是如何setTextColor输入字符串呢?

2 个答案:

答案 0 :(得分:0)

使用SpannableString,例如:

final String s = editText.getText().toString();
final Spannable spannable = new SpannableString(s);
spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

更新endIndex使其指向所讲单词的结尾,

答案 1 :(得分:0)

也这样使用

String name = "Test String"; 
textView.setText( Html.fromHtml(String.format("<font color=\"#%s\">text</font>", name ), TextView.BufferType.SPANNABLE));