前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android 倒计时 仿京东

Android 倒计时 仿京东

作者头像
yechaoa
发布于 2022-06-10 05:18:15
发布于 2022-06-10 05:18:15
28200
代码可运行
举报
文章被收录于专栏:移动开发专栏移动开发专栏
运行总次数:0
代码可运行

效果

xml

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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、计算时间
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
* 倒计时
*/
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
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
   //开启倒计时
   handler.sendEmptyMessage(0);
3、handler中处理
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            countDown();
            sendEmptyMessageDelayed(0, 1000);
        }
    };
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 效果
  • xml
  • shape
  • java
    • 1、计算时间
    • 2、在需要的地方调用handler
    • 3、handler中处理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档