在线程和Runnable接口的Andr​​oid更新的TextView [英] Android update TextView in Thread and Runnable

查看:244
本文介绍了在线程和Runnable接口的Andr​​oid更新的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?

这里的code(方法称为启动):

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线程更新。你需要一个处理器,张贴到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();
}

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

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