十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
新的商品详情,对特卖要增加倒计时的功能:
创新互联致力于互联网网站建设与网站营销,提供网站制作、成都网站建设、网站开发、seo优化、网站排名、互联网营销、成都小程序开发、公众号商城、等建站开发,创新互联网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。
采用了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:
*
*
* 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
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;
}
}
文章题目:android倒计时实现
网页地址:http://shouzuofang.com/article/ghggge.html