2013-04-04 67 views
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方法逐個顏色單詞。處理程序發佈延遲不適用於循環

謝謝。

+0

你想同色FO個個當用戶點擊 – Raghunandan 2013-04-04 07:32:16

+0

我想陸續顏色由一個每個字在屏幕上用2第二過渡時間的話 – 2013-04-04 07:39:10

回答

0

可以是可以嘗試這一點:在間隔調用setTextColor()方法遞歸地爲2秒

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

是的,遞歸的作品,但爲什麼我的是不工作? – 2013-04-04 08:04:29

+0

,因爲你並不等待處理程序完成它的任務,即在for-each循環中將句子的部分着色爲紅色,並且隨着迭代的繼續進行以及處理程序作用域的創建和完成。只有在最後一次迭代中,處理程序纔會執行並完成它的任務。 – KunalK 2013-04-04 08:22:23

0

與綠色的顏色作爲foregr0und單詞每1秒鐘的顯示以下的話。 Textview會有你好!首先用綠色顯示,延時1秒後,下一個單詞將以綠色附加到文本視圖。這重複,直到它達到字符串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. 

} 

}); 
+0

@oguz kilinc嘗試以上 – Raghunandan 2013-04-04 08:20:42

+0

我會嘗試,但我想知道,在該代碼中,我可以暫停和播放着色 – 2013-04-04 08:39:48

+0

我不明白你的意思是暫停和播放着色。 – Raghunandan 2013-04-04 09:19:42