Android 倒计时 仿京东

效果

这里写图片描述

xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/dp_10"
        android:text="距结束"/>

    <TextView
        android:id="@+id/tv_seckill_hour"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_bg_black_corners"
        android:paddingLeft="@dimen/dp_4"
        android:paddingRight="@dimen/dp_4"
        android:text="00"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" : "/>

    <TextView
        android:id="@+id/tv_seckill_minute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_bg_black_corners"
        android:paddingLeft="@dimen/dp_4"
        android:paddingRight="@dimen/dp_4"
        android:text="00"
        android:textColor="@color/white"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" : "/>

    <TextView
        android:id="@+id/tv_seckill_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_bg_black_corners"
        android:paddingLeft="@dimen/dp_4"
        android:paddingRight="@dimen/dp_4"
        android:text="00"
        android:textColor="@color/white"/>

</LinearLayout>

shape

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <solid android:color="@color/black"/>
</shape>

java

1、计算时间
/**
* 倒计时
*/
private void countDown() {
   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
   Date curDate = new Date(System.currentTimeMillis());
   String format = df.format(curDate);
   try {
       //时间换算
       Date date = df.parse(mEndTime);
       Date date1 = df.parse(format);
       long defTime = date.getTime() - date1.getTime();
       long days = defTime / (1000 * 60 * 60 * 24);
       long hours = (defTime - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
       long minute = (defTime - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
       long seconds = defTime % 60000;
       long second = Math.round((float) seconds / 1000);

       //不足10的补0
       if (hours >= 10) mTvSeckillHour.setText(String.valueOf(hours));
       else mTvSeckillHour.setText(String.valueOf("0" + hours));

       if (minute >= 10) mTvSeckillMinute.setText(String.valueOf(minute));
       else mTvSeckillMinute.setText(String.valueOf("0" + minute));

       if (second >= 10) mTvSeckillSecond.setText(String.valueOf(second));
       else mTvSeckillSecond.setText(String.valueOf("0" + second));

   } catch (ParseException e) {
       e.printStackTrace();
   }
}
2、在需要的地方调用handler
   //开启倒计时
   handler.sendEmptyMessage(0);
3、handler中处理
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            countDown();
            sendEmptyMessageDelayed(0, 1000);
        }
    };


更多:60s倒计时

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yechaoa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值