您好,登錄后才能下訂單哦!
新的商品詳情,對特賣要增加倒計時的功能:
采用了android系統的CountDownTimer這個類
關于這個類的用法:
* Schedule a countdown until a time in the future, with
* regular notifications on intervals along the way.
*
* Example of showing a 30 second countdown in a text field:
*
* <pre class="prettyprint">
* new CountDownTimer(30000, 1000) {
*
* public void onTick(long millisUntilFinished) {
* mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
* }
*
* public void onFinish() {
* mTextField.setText("done!");
* }
* }.start();
在時間工具類中加了兩個方法:
/**
* 獲得倒計時的時間間隔
*
* @param dateStr
* @return
*/
public static long getCountdownInterval(String dateStr) {
long interval = 0;
if (StringUtils.isBlank(dateStr)) {
return interval;
}
Date countdownDate = new Date();
try {
countdownDate = DB_DATE_FORMAT.parse(dateStr);
} catch (ParseException e) {
LogUtils.loge("DateUtils", "日期格式有問題");
}
Date now = new Date();
interval = countdownDate.getTime() - now.getTime();
if (interval < 0)
return 0;
return interval;
}
/**
* @param createTime
* @return 返回離當前時刻的時間間隔,以中文形式描述
*/
public static int[] getCountdownIntervalDetails(long intervalSec) {
int[] countdownIntervalDetals = new int[4];
intervalSec = intervalSec / 1000;// 先轉成秒
// 天
int day = (int) (intervalSec / (24 * 3600));
countdownIntervalDetals[0] = day;
intervalSec = intervalSec - day * (24 * 3600);
// 時
int hour = (int) (intervalSec / 3600);
countdownIntervalDetals[1] = hour;
intervalSec = intervalSec - hour * 3600;
// 分
int min = (int) (intervalSec / 60);
countdownIntervalDetals[2] = min;
intervalSec = intervalSec - min * 60;
countdownIntervalDetals[3] = (int) intervalSec;
return countdownIntervalDetals;
}
最后,組裝成一個控件:
/**
* 倒計時控件
*/
public class L_CountdownBlock implements Y_UIBlock {
private Context mContext;
private View container;
private CountDownTimer cdt;
private TextView tv_countdown_day;
private TextView tv_countdown_hour;
private TextView tv_countdown_min;
private TextView tv_countdown_sec;
protected int[] intervalDetails;
public L_CountdownBlock(View view) {
this.container = view;
this.mContext = container.getContext();
}
/**
* 設置結束時間
*
* @param dateStr
*/
private void setDate(String dateStr) {
long interval = DateUtils.getCountdownInterval(dateStr);
cdt = new CountDownTimer(interval, 1000) {
@Override
public void onTick(long millisUntilFinished) {
intervalDetails = DateUtils
.getCountdownIntervalDetails(millisUntilFinished);
tv_countdown_day.setText(intervalDetails[0] + "");
tv_countdown_hour.setText(intervalDetails[1] + "");
tv_countdown_min.setText(intervalDetails[2] + "");
tv_countdown_sec.setText(intervalDetails[3] + "");
}
@Override
public void onFinish() {
//TODO 加了倒計時結束的邏輯
}
};
cdt.start();
}
/**
* 取消倒計時
*/
private void cancelCountdown() {
if (null != cdt) {
cdt.cancel();
}
}
@Override
public <T> void update(T t) {
tv_countdown_day = (TextView) container
.findViewById(R.id.tv_countdown_day);
tv_countdown_hour = (TextView) container
.findViewById(R.id.tv_countdown_hour);
tv_countdown_min = (TextView) container
.findViewById(R.id.tv_countdown_min);
tv_countdown_sec = (TextView) container
.findViewById(R.id.tv_countdown_sec);
setDate((String) t);
}
@Override
public View getContentView() {
return container;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。