处理后的延迟不循环工作, [英] Handler post delay not work in for loop

查看:95
本文介绍了处理后的延迟不循环工作,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    Spannable WordtoSpan;
TextView tvView;
public void btnStart(View v)
{
    tvView = (TextView)findViewById(R.id.tvTest);
    changeColorOfText("I know just how to whisper, And I know just how to cry,I know just where to find the answers.");

}
int sIndexOfWord;
int fIndexOfWord;
private void changeColorOfText(String sentences)
{
    String[] arrWords = sentences.split(" ");
    WordtoSpan = new SpannableString(sentences);

    int k = 1;
    for(String word : arrWords) {

        sIndexOfWord = sentences.indexOf(word);
        fIndexOfWord = sIndexOfWord + word.length();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() { 
             public void run() { 
                WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW), sIndexOfWord, fIndexOfWord, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvView.setText(WordtoSpan); 
             } 
        }, 2000*k++);
    }

}

这code没有工作,只是颜色一句的最后文本。我怎么能由一个与handler.postDelayed方法颜色词之一。

This code not work, just color the last text of sentence. How can i color word one by one with handler.postDelayed method.

感谢。

推荐答案

与绿色作为foregr0und的话每1秒显示在下面的话。
TextView中会有您好!与绿色和1秒下一个字的延迟之后将被附加到与绿色TextView的第一显示。此重复直到它到达字符串s的长度。如果你不希望这个新词的只是设置文本追加你
_tv.setText(文本);并删除追加的部分。

The below displays words with green color as the foregr0und for words every 1 second. Textview will have hello! displayed first with green color and after a delay for 1 second next word will be appended to the textview with green color. This repeats till it reaches the length of string s. If you don't want to append you just set text of the new word _tv.setText(text); and remove the append part.

此外,我已经使用了一个定时器,并设置UI线程上的文字。你也可以使用一个处理程序。

Also i have used a timer and set the text on the ui thread. You can also use a handler.

String s;
int _count=0;//counter
Timer   _t;
TextView _tv;
String[] each;//holds each word in an array
Button b;
b= (Button) findViewById(R.id.button1);
_tv = (TextView) findViewById( R.id.tv );
b.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        //s=et.getText().toString();
        s="hello!. how are you";
        _tv.setText("");
        for(int i=0;i<s.length();i++)
        {
            each = s.split("\\s+");
        }
     _t = new Timer();

        _t.scheduleAtFixedRate( new TimerTask() {
                @Override
                public void run() {


                    runOnUiThread(new Runnable() //run on ui thread
                     {
                      public void run() 
                      { 
                          if(_count<=each.length-1)
                          {
                          SpannableString text = new SpannableString(each[_count]);  

                        text.setSpan(new ForegroundColorSpan(Color.GREEN), 0,text.length(), 0);  
                        _tv.append(text);
                        _tv.append(" ");
                         System.out.println("................"+each[_count]);
                        _count=_count+1;


                     }
                          else
                          {
                              _t.cancel();
                          }
                      }

                     });
                }
            }, 1000, 1000 ); //change to 2000 for 2 second delay.

}

});

这篇关于处理后的延迟不循环工作,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆