实现在Android的反 [英] Implementing counter in Android

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

问题描述

我有,我需要展示柜台,从3比1,然后迅速切换到另一个活动的应用程序。将TimerTask的将是适合这样做呢?任何人都可以告诉我究竟一个例子怎么办呢?

CountDownTimer的工作。 $ C $下显示3秒计时器。

 新CountDownTimer(4000,1000){

             公共无效onTick(长millisUntilFinished){
                 动画myFadeOutAnimation = AnimationUtils.loadAnimation(countdown.this,R.anim.fadeout);
                 counter.startAnimation(myFadeOutAnimation);
                 counter.setText(Long.toString(millisUntilFinished / 1000));
             }

             公共无效onFinish(){
                 counter.setText(完成了!);
             }
        }。开始();
 

解决方案

我会更好地使用 CountDownTimer

如果你想例如用户计数器计数3秒时:

  //新的计数器计数3000毫秒带勾每1000毫秒
CountDownTimer myCountDown =新CountDownTimer(3000,1000){
    公共无效onTick(长millisUntilFinished){
        //更新为新的计在UI
    }

    公共无效onFinish(){
        //启动活动
   }
};
//启动倒计时
myCountDown.start();
 

I have got an application where I need to show counter from 3 to 1 then quickly switch to another activity. Will TimerTask will be suitable for doing this? Can anybody show me an example of exactly how to do it?

CountDownTimer Worked. Code for showing timer for 3 seconds is.

new CountDownTimer(4000, 1000) {

             public void onTick(long millisUntilFinished) {
                 Animation myFadeOutAnimation = AnimationUtils.loadAnimation(countdown.this, R.anim.fadeout);       
                 counter.startAnimation(myFadeOutAnimation);
                 counter.setText(Long.toString(millisUntilFinished / 1000));
             }

             public void onFinish() {
                 counter.setText("done!");
             }
        }.start();

解决方案

I would better use a CountDownTimer.

If you want for example your counter to count 3 seconds:

//new Counter that counts 3000 ms with a tick each 1000 ms
CountDownTimer myCountDown = new CountDownTimer(3000, 1000) {
    public void onTick(long millisUntilFinished) {
        //update the UI with the new count
    }

    public void onFinish() {
        //start the activity
   }
};
//start the countDown
myCountDown.start();

这篇关于实现在Android的反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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