CountDownTimer是如何访问UI OnTick方法里面? [英] How is CountDownTimer accessing UI inside OnTick method?

查看:400
本文介绍了CountDownTimer是如何访问UI OnTick方法里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何 CountDownTimer 正在访问UI onTick 法里面?

 (新CountDownTimer(10000,1000){

    @覆盖
    公共无效onFinish(){
        // TODO自动生成方法存根

    }

    @覆盖
    公共无效onTick(长millisUntilFinished){
        TextView的电视=(TextView中)findViewById(R.id.tvLCD);
        tv.setText(Long.toString(millisUntilFinished));
    }
})。开始();
 

解决方案

您可以通过<从一个线程访问UI href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29"相对=nofollow> Activity.runOnUiTread() ,的 View.post() ,<一个href="http://developer.android.com/reference/android/view/View.html#postDelayed%28java.lang.Runnable,%20long%29"相对=nofollow> View.postDelayed() 或通过的 处理程序 CountDownTimer 使用处理程序用于此目的(<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.3.4_r1/android/os/CountDownTimer.java#109"相对=nofollow>来源)。

阅读这篇文章了解如何使用所有的这些方法。

How CountDownTimer is accessing UI inside onTick method?

(new CountDownTimer(10000,1000){

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTick(long millisUntilFinished) {
        TextView tv = (TextView)findViewById(R.id.tvLCD);
        tv.setText(Long.toString(millisUntilFinished));
    }           
}).start();

解决方案

You can get access to UI from thread by Activity.runOnUiTread(), View.post(), View.postDelayed() or via Handler. CountDownTimer uses Handler for this purpose (source).

Read this article for understanding how to use all of these methods.

这篇关于CountDownTimer是如何访问UI OnTick方法里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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