Android -- 倒计时的实现

CountDownTimer这个抽象类,实现了倒计时的功能。将后台线程的创建和Handler队列封装成一个方便的类调用。

这个类比较简单,只有四个方法:onTick,onFinsh、cancel和start。其中前面两个是抽象方法,所以要重写一下。


1.继承 CountDownTimer,重载onTick()和onFinish() 两个方法,并实现构造方法

2.调用start()方法实现倒计时


下面是官方给的一个小例子:

new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

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

package com.leigo.demo;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.leigo.demo.receiver.HeadsetDetectReceiver;


public class MainActivity extends Activity implements View.OnClickListener {

    private TextView mTextField;
    private Button mBtnStart;
    private Button mBtnCancel;

    private MyCountDownTimer mCountDownTimer;

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

        mTextField = (TextView) findViewById(R.id.textField);
        mBtnStart = (Button) findViewById(R.id.btn_start);
        mBtnCancel = (Button) findViewById(R.id.btn_cancel);

        mBtnStart.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);

        mCountDownTimer = new MyCountDownTimer(30000, 1000);

    }

    private class MyCountDownTimer extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mTextField.setText("请等待(" + millisUntilFinished / 1000 + ")秒");
        }

        @Override
        public void onFinish() {
            mTextField.setText("完成!");
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start:
                mCountDownTimer.start();
                break;
            case R.id.btn_cancel:
                mCountDownTimer.cancel();
                break;
        }
    }
}

onFinish()中的代码是计时器结束的时候要做的事情

onTick(Long m)中的代码是你倒计时开始时要做的事情,参数m是完成的时间,

构造方法MyCount()中的两个参数中,前者是倒计的时间数,后者是倒计每秒中间的间隔时间,都是以毫秒为单位。例如要倒计时30秒,每秒中间间隔时间是1秒,两个参数可以这样写MyCount(30000,1000)。 

当你想取消的时候使用mc.cancel()方法就行了。




你可能感兴趣的:(Android -- 倒计时的实现)