1. 程式人生 > 實用技巧 >Android中實現倒計時的幾種方式

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);
    }