我在TextView中有一个文本,需要突出显示每个句子3秒钟。我尝试了Thraed计时器和handler.postDelayed。但是没有在循环中匆匆忙忙。
如何在FOR循环中实现延迟?
我的代码:
Button btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView txtMain = (TextView) findViewById(R.id.txtMain);
String textString = txtMain.getText().toString();
ArrayList<Integer> Sentences = new ArrayList<Integer>();
// Save indexes of sentences in array
Sentences.add(0);
for (int i = 0; i < textString.length(); i++) {
if (textString.charAt(i) == '.') {
Sentences.add(i);
}
}
// Need 3 seconds delay at the end of this loop
for (int j = 0; j < Sentences.size() - 1; j++) {
Spannable spanText = Spannable.Factory.getInstance().newSpannable(textString);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), Sentences.get(j), Sentences.get(j + 1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtMain.setText(spanText);
}
}
});
提前致谢