我想让我的可运行设备每.75秒更新一次我的UI,我不想使用AsyncTask。但是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);,因此只能在for循环的末尾设置TextView

在将文本真正设置为textview之前,您的线程将进入睡眠状态。我认为您应该每750ms调用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() {
 }


}。开始();

关于android - 可运行的SetText View 无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12273553/

10-10 10:21