Android 倒计时 仿京东

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yechaoa/article/details/82388519

效果

这里写图片描述

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倒计时

猜你喜欢

转载自blog.csdn.net/yechaoa/article/details/82388519