大佬教程收集整理的这篇文章主要介绍了android – 按下时在文本视图中突出显示单词,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的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更好的方法吗?
我的ClickableSpan:
final int almostBlack = m_resources.getColor(R.color.i_black_almost); ClickableSpan readWord = new ClickableSpan() { private int almostBlack = AlmostBlack; public void onClick(View view) { TextView v = (TextView) view; v.setTextColor(almostBlack); ...
在TTS回调时的处理程序中:
@H_887_9@m_SentenceView.setTextColor(m_resources.getColor(R.color.i_black));
如果你想做类似的事情而不等待TTS或任何回调,你可以使用颜色状态列表在按下或释放视图时触发颜色变化:
颜色状态列表res / color / clickable_words.xml:
<?xml version="1.0" encoding="utf-8"?> <SELEctor xmlns:android="http://scheR_697_11845@as.android.com/apk/res/android" > <item android:color="@color/i_black_almost" android:state_pressed="true"/> <item android:color="@color/i_black" /> </SELEctor>
TextView:
<TextView android:id="@+id/sentence" ... android:textColor="@color/clickable_words" android:textColorLink="@color/clickable_words" android:textColorHighlight="@color/i_blue" />
以上是大佬教程为你收集整理的android – 按下时在文本视图中突出显示单词全部内容,希望文章能够帮你解决android – 按下时在文本视图中突出显示单词所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。