我想让我的可运行对象每 .75 秒更新一次我的 UI,我不想使用 AnsyTask。但是 TextView 仅在 for 循环的末尾设置,知道为什么吗?
...
robotWords = "........Hey hello user!!!";
wordSize = robotWords.length();
mHandler.postDelayed(r, 750);
}
private Runnable r = new Runnable()
{
public void run()
{
for(int i=0; i<wordSize; i++)
{
robotTextView.setText("why this words only display on the textView at last operation on this for loop?");
Log.i(TAG, robotWords.substring(0, i));
try
{
Thread.sleep(750);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
};
最佳答案
由于这一行 Thread.sleep(750);
在文本真正设置到您的 TextView 之前,您的线程将休眠。我认为您应该每 750 毫秒调用一次 Handler.postDelayed 而不是使用 Thread.sleep(750);
或使用 CountDownTimer
new CountDownTimer(750 * wordSize, 750) {
public void onTick(long millisUntilFinished) {
robotTextView.setText("why this words only display on the textView at last operation on this for loop?");
Log.i(TAG, robotWords.substring(0, i));
}
public void onFinish() {
}
}.start();
关于android - Runnable 中的 SetText View 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12273553/