CountDownTimer是如何访问UI OnTick方法里面?里面、方法、CountDownTimer、UI

由网友(败类)分享简介:如何 CountDownTimer 正在访问UI onTick 法里面?(新CountDownTimer(10000,1000){@覆盖公共无效onFinish(){// TODO自动生成方法存根}@覆盖公共无效onTick(长millisUntilFinished){TextView的电视=(TextView中)f...

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

 (新CountDownTimer(10000,1000){

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

    }

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

解决方案

您可以通过 Activity.runOnUiTread() ,的 View.post() View.postDelayed() 或通过的 处理程序 CountDownTimer 使用处理程序用于此目的(来源)。

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

Countdown Timer

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.

阅读全文

相关推荐

最新文章