Android 倒计时 仿京东
        【摘要】 
                    
                        
                    
                    效果 
 
xml 
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_co...
    
    
    
    效果

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>
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
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>
  
 - 1
- 2
- 3
- 4
- 5
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();
   }
}
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
2、在需要的地方调用handler
   //开启倒计时
   handler.sendEmptyMessage(0);
  
 - 1
- 2
3、handler中处理
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            countDown();
            sendEmptyMessageDelayed(0, 1000);
        }
    };
  
 - 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
 更多:60s倒计时 
 
文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/yechaoa/article/details/82388519
        【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
            cloudbbs@huaweicloud.com
        
        
        
        
        
        
        - 点赞
- 收藏
- 关注作者
 
             
           
评论(0)