处理程序后延迟不适用于for循环

时间:2013-04-04 07:18:34

标签: android handler

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

}

此代码不起作用,只是为句子的最后一个文本着色。我怎么能用handler.postDelayed方法逐字着色。

感谢。

2 个答案:

答案 0 :(得分:0)

可能你可以试试这个:以2秒的间隔递归调用setTextColor()方法

int k = 0;
private void setTxtColor(){
  if(k < arrWords.length){
      Handler handler = new Handler();
      handler.postDelayed(new Runnable() { 
           public void run() {
              sIndexOfWord = sentences.indexOf(arrWords[k]);
              fIndexOfWord = sIndexOfWord + word.length(); 
              WordtoSpan.setSpan(new BackgroundColorSpan(Color.YELLOW), sIndexOfWord, fIndexOfWord, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
              tvView.setText(WordtoSpan);
              k++;
              setTxtColor(); 
           } 
      }, 2000);
  }
}

答案 1 :(得分:0)

下面每1秒显示绿色字样作为foregr0und字样。 Textview会有你好!首先显示绿色,延迟1秒后,下一个字将以绿色附加到textview。这种情况一直重复到达弦s的长度。如果您不想追加,只需设置新单词的文本即可 _tv.setText(文本);并删除附加部分。

我也使用了计时器并在ui线程上设置了文本。您也可以使用处理程序。

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.

}

});