Android 在 Thread 和 Runnable 中更新 TextView [英] Android update TextView in Thread and Runnable

查看:21
本文介绍了Android 在 Thread 和 Runnable 中更新 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Android 中制作一个简单的计时器,每秒更新一个 TextView.它只是像扫雷一样计算秒数.

I want to make a simple timer in Android that updates a TextView every second. It simply counts seconds like in Minesweeper.

问题是当我忽略 tvTime.setText(...) 时(使其成为//tvTime.setText(...),在 LogCat 中将每秒打印以下数字.但是当我想将此数字设置为 TextView(在另一个线程中创建)时,程序会崩溃.

The problem is when i ignore the tvTime.setText(...) (make it //tvTime.setText(...), in LogCat will be printed the following number every second. But when i want to set this number to a TextView (created in another Thread), the program crashes.

有人知道如何轻松解决这个问题吗?

Does anyone have an idea how to solve this easily?

代码如下(启动时调用方法):

Here's the code (method is called on startup):

private void startTimerThread() {
    Thread th = new Thread(new Runnable() {
        private long startTime = System.currentTimeMillis();
        public void run() {
            while (gameState == GameState.Playing) {
                System.out.println((System.currentTimeMillis() - this.startTime) / 1000);
                tvTime.setText("" + ((System.currentTimeMillis() - this.startTime) / 1000));
                try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    th.start();
}

终于,我明白了.以下是解决方案,有兴趣的朋友可以参考一下.

Finally, I got it. Here is the solution, for those who are interested in.

private void startTimerThread() {       
    Thread th = new Thread(new Runnable() {
        private long startTime = System.currentTimeMillis();
        public void run() {
            while (gameState == GameState.Playing) {                
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvTime.setText(""+((System.currentTimeMillis()-startTime)/1000));
                    }
                });
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    th.start();
}

推荐答案

UserInterface 只能由 UI Thread 更新.你需要一个 Handler,发布到 UI 线程:

The UserInterface can only be updated by the UI Thread. You need a Handler, to post to the UI Thread:

private void startTimerThread() {
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        private long startTime = System.currentTimeMillis();
        public void run() {
            while (gameState == GameState.Playing) {  
                try {
                    Thread.sleep(1000);
                }    
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable(){
                    public void run() {
                       tvTime.setText("" + ((System.currentTimeMillis() - this.startTime) / 1000));
                }
            });
            }
        }
    };
    new Thread(runnable).start();
}

这篇关于Android 在 Thread 和 Runnable 中更新 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆