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方法逐個顏色單詞。處理程序發佈延遲不適用於循環
謝謝。
你想同色FO個個當用戶點擊 – Raghunandan 2013-04-04 07:32:16
我想陸續顏色由一個每個字在屏幕上用2第二過渡時間的話 – 2013-04-04 07:39:10