带延迟周期的循环

带延迟周期的循环

我有一个 TextView,我希望每一秒都突出显示单词中的另一个字母。

例如:
h e l l o - h e l l l o - h e l l o - h e l l o - h e l l l o ojit_r

我做了什么:

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);
    }
};

不幸的是它不起作用,我只能看到突出显示的最后一个字母。

先感谢您。

最佳答案

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);
    }
};

关于android - 带延迟周期的循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17392046/

10-10 03:48