我有一个TextView用于显示段落文本,并希望当单词被按下时,使用TTS让我的应用程序朗读它。如果单词在按下时可以被突出显示会更好。我已经使用ClickableSpan实现了每个单词的点击效果。它几乎可以正常工作,但是我不知道如何在播放完成后将突出显示的状态恢复为正常状态。每次我点击一个新单词时,以前的单词就会失去突出显示,而新单词则会被突出显示,但是我不知道如何移除TTS回调后的突出显示状态:
我的TextView:
为了填充TextView,我使用以下代码:
有没有一种方法可以做到这一点?或者是否有比ClickableSpan更好的方法?
我的TextView:
<TextView
android:id="@+id/sentence"
...
android:textColorHighlight="@color/i_blue"
/>
为了填充TextView,我使用以下代码:
SpannableStringBuilder strBuilder = new SpannableStringBuilder();
Iterator<Word> iterator = e.getWordList().iterator();
int wordStart, wordEnd;
while (iterator.hasNext()) {
Word w = iterator.next();
wordStart = strBuilder.length() + w.getPrefix().length();
wordEnd = wordStart + w.getWord().length();
strBuilder.append(w.getPrefix() + w.getWord() + w.getSuffix());
final String currentWord = w.getWord();
ClickableSpan readWord = new ClickableSpan() {
private String clickedWord = currentWord;
public void onClick(View view) {
Message msg = m_HandlerReadWord.obtainMessage();
msg.obj = clickedWord;
m_HandlerReadWord.sendMessage(msg);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
strBuilder.setSpan(readWord, wordStart, wordEnd, 0);
}
m_SentenceView.setText(strBuilder);
m_SentenceView.setMovementMethod(LinkMovementMethod.getInstance());
我还有一个方法,当TTS播放完单词后,它会回调这个方法:
public void resetHighlight() {
//What can I do there to reset any highlighted word?
}
有没有一种方法可以做到这一点?或者是否有比ClickableSpan更好的方法?