1. 程式人生 > >Android 倒計時 仿京東

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);
        }
    };