新的商品详情,对特卖要增加倒计时的功能:
采用了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。