android 时间倒计时,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"/>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值