1. 程式人生 > >Android 開發—支付倒計時功能

Android 開發—支付倒計時功能

佈局介面關鍵程式碼如下,其中的bg_time是顯示時間的底圖

 <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/dp_6"
                                android:orientation="horizontal">

                                <TextView
                                    android:id="@+id/tv_activity_day"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:background="@drawable/bg_time"
                                    android:gravity="center"
                                    android:text="4"
                                    android:textColor="@color/colorWhite"
                                    android:textSize="@dimen/sp_12" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="@dimen/dp_3"
                                    android:layout_marginRight="@dimen/dp_3"
                                    android:gravity="center"
                                    android:text="天"
                                    android:textColor="@color/colorAccent"
                                    android:textSize="@dimen/sp_12" />

                                <TextView
                                    android:id="@+id/tv_activity_hour"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:background="@drawable/bg_time"
                                    android:gravity="center"
                                    android:text="4"
                                    android:textColor="@color/colorWhite"
                                    android:textSize="@dimen/sp_12" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="@dimen/dp_3"
                                    android:layout_marginRight="@dimen/dp_3"
                                    android:gravity="center"
                                    android:text="時"
                                    android:textColor="@color/colorAccent"
                                    android:textSize="@dimen/sp_12" />

                                <TextView
                                    android:id="@+id/tv_activity_min"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:background="@drawable/bg_time"
                                    android:gravity="center"
                                    android:text="4"
                                    android:textColor="@color/colorWhite"
                                    android:textSize="@dimen/sp_12" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="@dimen/dp_3"
                                    android:layout_marginRight="@dimen/dp_3"
                                    android:gravity="center"
                                    android:text="分"
                                    android:textColor="@color/colorAccent"
                                    android:textSize="@dimen/sp_12" />

                                <TextView
                                    android:id="@+id/tv_activity_second"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:background="@drawable/bg_time"
                                    android:gravity="center"
                                    android:text="4"
                                    android:textColor="@color/colorWhite"
                                    android:textSize="@dimen/sp_12" />

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="@dimen/dp_3"
                                    android:layout_marginRight="@dimen/dp_3"
                                    android:gravity="center"
                                    android:text="秒"
                                    android:textColor="@color/colorAccent"
                                    android:textSize="@dimen/sp_12" />
                            </LinearLayout>

主要是在activity中的呼叫,關鍵程式碼如下:

private void startGroupCountDown(final String endTime) {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        mTimer = new Timer();
        if (mTimerTask != null) {
            mTimerTask.cancel();
        }
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        setCountDown(endTime);
                    }
                });
            }
        };
        mTimer.schedule(mTimerTask, 0, 1000); //0s後執行task,經過1s再次執行
    }

    private void setCountDown(final String endTime) {
        String mCurrentTime = DateTimeUtil.getCurrentDate(DateTimeUtil.FORMAT_YMD_HMS);
        int dateCompare = DateTimeUtil.compareTime(mCurrentTime, endTime, DateTimeUtil.FORMAT_YMD_HMS);
        if (dateCompare == -1) {
            String distanceTime[] = DateTimeUtil.getDistanceTime(mCurrentTime, endTime);
            for (int i = 1; i < distanceTime.length; i++) {
                if (distanceTime[i].length() == 1) {
                    distanceTime[i] = "0" + distanceTime[i];
                }
            }
            mDayTV.setText(distanceTime[0]);
            mHourTV.setText(distanceTime[1]);
            mMinTV.setText(distanceTime[2]);
            mSecondTV.setText(distanceTime[3]);

        } else {
            mDayTV.setText("0");
            mHourTV.setText("0");
            mMinTV.setText("0");
            mSecondTV.setText("0");
            //關閉頁面
            finish();
        }
    }

當然了,主要思路是利用TimerTask和timer每隔1S執行達到重新整理當前時間,用當前時間和支付訂單成功返回時間做差(需要自己寫一個時間工具類,網上很多就不多說了),我這裡刪除了day和hour效果後如下: 效果