Android 时间倒计时

Android时间倒计时在网上一搜就一堆,而且也经常用到。备份一下,以便下次直接使用

1、首先我创建一个接口,为什么要创建一个接口呢?因为我是建立了一个类继承CountDownTimer。这样做的意义就是不用每次

需要用到的倒计时的时候不需要在Activity里面创建直接调用就可以了。

public interface OnCountDownTimeListener {
    void getCountDownTime(int time);
    void timeOver();

}

2、倒计时的实现类如下:

public class CountDownThread extends CountDownTimer {
    private final static  String TAG = CountDownThread.class.getSimpleName();
    /**
     * @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.
     */


    private OnCountDownTimeListener listener;


    Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
//                    Log.i(TAG, "还剩"+msg.arg1+"秒");
                    listener.getCountDownTime(msg.arg1);
                    break;
                case 2:
//                    Log.i(TAG, "倒计时结束");
                    listener.timeOver();
                    break;
            }


        }
    };


    public CountDownThread(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }


    public void setOnCountDownTimeListener(OnCountDownTimeListener listener){
        this.listener = listener;
    }


    @Override
    public void onTick(long millisUntilFinished) {
//        Log.i(TAG, "还剩"+millisUntilFinished/1000+"秒");
        Message msg = new Message();
        msg.what = 1;
        msg.arg1 = (int)millisUntilFinished/1000;
        mHandler.sendMessage(msg);
    }


    @Override
    public void onFinish() {


        Message msg = new Message();
        msg.what = 2;
        mHandler.sendMessage(msg);
    }
}

3、在Activity的调用,其中使用了butterknife

public class CountDownActivity extends Activity implements OnCountDownTimeListener{



    private final static String TAG = CountDownActivity.class.getSimpleName();
    private CountDownThread countDownThread;
    @BindView(R.id.count_down)
    TextView textView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_down);
        ButterKnife.bind(this);
        startCountDownTime(5);
    }


    //开始时间倒计时
    private void startCountDownTime(int time) {
        if (countDownThread != null) {
            countDownThread.cancel();
        }
        countDownThread = new CountDownThread(time * 1000, 1000);
        countDownThread.setOnCountDownTimeListener(this);
        countDownThread.start();
    }


    @Override
    public void getCountDownTime(int time) {
        textView.setText(String.valueOf(time));
    }


    @Override
    public void timeOver() {
        textView.setText(String.valueOf("倒计时结束"));
    }

}

4、相应的布局


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
            android:id="@+id/count_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello world"/>

你可能感兴趣的:(Android)