布局界面关键代码如下,其中的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效果后如下: