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、相应的布局

<?xml version="1.0" encoding="utf-8"?>
    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"/>

更多相关文章

  1. Android应用程序设置系统时间的方法
  2. Android Studio自动化快速实现Parcelable接口序列化
  3. Android 接口定义语言AIdl
  4. Android中如何修改系统时间(应用程序获得系统权限)
  5. Android(安卓)时间戳和日期之间的转化
  6. 微信小程序时间转换
  7. Android 如何提前知道fling velocity的距离、时间 根据滑动距离
  8. php做接口+android 请求API接口并展示到ListView例子

随机推荐

  1. Android的内存优化的几种方案
  2. Android(安卓)相机(Camera)拍照入门(一)
  3. 一位面试官写给初学Android的同学们
  4. Android蓝牙开发—经典蓝牙和BLE(低功耗)蓝
  5. 我是一名Coder
  6. Android开发从零开始之java-数据类型
  7. Android(安卓)7.0 行为变更
  8. Android中MVP模式讲解及实践
  9. Android-小巫新闻客户端底部菜单切换实现
  10. 微信授权APP第三方登录(Android)