我有一个TextView,我希望每一秒都能突出显示单词中的另一个字母。
例如: h ello - h e llo - 他 l lo - hel l o - 地狱 o
我做了什么:
int i = 0;
String text;
Handler handler = new Handler();
public void spanText(String txt) {
text = txt;
for(int i=0; i<text.length(); i++) {
handler.post(runnable);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
Spannable spannable = Spannable.Factory.getInstance().newSpannable(text);
StyleSpan style = new StyleSpan(Typeface.BOLD);
spannable.setSpan(style, i, i+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
bestResult.setText(spannable, BufferType.SPANNABLE);
i++;
if(i < text.length())
handler.postDelayed(runnable, 5000);
}
};
不幸的是它不起作用,我只能看到突出显示的最后一个字母。
提前谢谢。
答案 0 :(得分:3)
int i = 0;
String text;
Handler handler = new Handler();
public void spanText(String txt) {
text = txt;
handler.post(runnable);
Runnable runnable = new Runnable() {
@Override
public void run() {
Spannable spannable = Spannable.Factory.getInstance().newSpannable(text);
StyleSpan style = new StyleSpan(Typeface.BOLD);
spannable.setSpan(style, i, i+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
bestResult.setText(spannable, BufferType.SPANNABLE);
i++;
if(i < text.length())
handler.postDelayed(runnable, 5000);
}
};