android - Runnable 中的 SetText View 无法正常工作

标签 android

我想让我的可运行对象每 .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 仅在 for 循环的末尾设置

在文本真正设置到您的 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/

相关文章:

android - 当方向处于横向模式时,android 中的 Admob SMART_BANNER 不起作用

android 网格布局实现

java - OpenGL 2.0 ES 矩阵堆栈如何工作?

Android 的 RelativeLayout 单元测试设置

android - 动画后的图片点击

android - 如何使用 robotium 测试 android 小部件

android - 如何防止为已回收的 View 生成不必要的工作线程?

android - 如何向选项卡标签添加填充?

android - 恢复应用内购买的安全问题

android - Android 中基于 CelID 的位置?