【发布时间】:2019-03-27 08:17:57
【问题描述】:
我有一个webview。我只是在string 中检索webview's 内容并将其显示在textview 中。在onTouch 事件中,文本将大声朗读。现在我想在文本正在讲话的onTouch 事件中逐字突出显示文本。对于textview,我们可以使用setTextColor。但是如何setTextColor 获取字符串呢?
【问题讨论】:
我有一个webview。我只是在string 中检索webview's 内容并将其显示在textview 中。在onTouch 事件中,文本将大声朗读。现在我想在文本正在讲话的onTouch 事件中逐字突出显示文本。对于textview,我们可以使用setTextColor。但是如何setTextColor 获取字符串呢?
【问题讨论】:
使用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 以指向正在说的任何单词的结尾,
【讨论】:
也可以这样使用
String name = "Test String";
textView.setText( Html.fromHtml(String.format("<font color=\"#%s\">text</font>", name ), TextView.BufferType.SPANNABLE));
【讨论】: