Android中实现倒计时的几种方式

1.使用 CountDownTimer 实现倒计时

/**
* CountDownTimer timer = new CountDownTimer(3000, 1000)中,
* 第一个参数表示总时间,第二个参数表示间隔时间。
* 意思就是每隔一秒会回调一次方法onTick,然后1秒之后会回调onFinish方法。
*/ 
CountDownTimer timer = new CountDownTimer(3000, 1000) { public void onTick(long millisUntilFinished) { txt.setText("倒计时" + millisUntilFinished / 1000 + "秒"); } public void onFinish() { Intent intent = new Intent(MainActivity.this, Main2Activity.class); startActivity(intent); } }; //调用 CountDownTimer 对象的 start() 方法开始倒计时,也不涉及到线程处理 timer.start();

 

2.利用Handler实现倒计时

 

private int count = 5;
    private TextView tv;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==100){
                if(count>0){
                    tv.setText(count+"");
                    count--;
                    handler.sendEmptyMessageDelayed(100,1000);
                }else {
                    Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                    startActivity(intent);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        tv = (TextView) findViewById(R.id.tv);
        handler.sendEmptyMessageDelayed(100,1000);
    }

 

3.利用动画实现倒计时

private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        ValueAnimator animator = ValueAnimator.ofInt(5,0);
        //设置时间
        animator.setDuration(5000);
        //均匀显示
        animator.setInterpolator(new LinearInterpolator());
        //监听
        animator.addUpdateListener(new AnimatorUpdateListener() {
            
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                tv.setText(value+"");
                if(value==0){
                    startActivity(new Intent(MainActivity.this,Second.class));
                }
            }
        });
        animator.start();
    }

 

4.利用Timer定时器

 

private TextView tv;    
    static int count = 5;

    Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            int count = msg.arg1;
            if(count==0){
                iv.setText("你好");
            }else {
                iv.setText(count+"");
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.iv);
        
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            
            @Override
            public void run() {
                Message message = new Message();
                message.arg1 = count;
                if(count!=-1){
                    count--;
                }else {
                    return;
                }
                handler.sendMessage(message);
            }
        }, 1000,1000);
    }

 

Android中实现倒计时的几种方式

上一篇:Android set与get/全局变量


下一篇:第四周Android练习