0
    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 方法一一着色。

谢谢。

4

2 回答 2

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);
  }
}
于 2013-04-04T07:51:11.417 回答
0

下面以绿色显示单词作为每 1 秒单词的前导。Textview会有你好!首先以绿色显示,延迟 1 秒后,下一个单词将以绿色附加到 textview 中。如此重复直到达到字符串 s 的长度。如果您不想附加,只需设置新单词的文本 _tv.setText(text); 并删除附加部分。

我还使用了一个计时器并在 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.

}

});
于 2013-04-04T07:57:44.077 回答