本文主要是介绍以分,秒,分秒形式的倒计时组合控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
实现方法也不是很难,在这里我用一个ViewGroup控件组合成了一个倒计时控件,通过CountDownTimer的实现类来做时间戳的换算 package com.kufeng.hj.one.view;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.kufeng.hj.one.R;
/**
* Created by Administrator on 2016/6/23.
* 定时器自定义组合控件
*/
public class CommodityBuyView extends RelativeLayout { //显示倒计时的格式如:00:00:00
private TextView tv_status;
//倒计的毫秒,以毫秒为单位 倒计时的总数
private long mMillisInFuture; //毫秒的最大算值
private long mCountDownInterval;
private MyCountDownTimer mcmc;
public CommodityBuyView(Context context, long mMillisInFuture, long mCountDownInterval) {
super(context);
this.mMillisInFuture = mMillisInFuture;
this.mCountDownInterval = mCountDownInterval;
if (mMillisInFuture > 0 && mCountDownInterval > 0)
initView(context);
}
public CommodityBuyView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public CommodityBuyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_view_commodi, this);
tv_status = ((TextView) view.findViewById(R.id.tv_announce_time));
mcmc = new MyCountDownTimer(mMillisInFuture, mCountDownInterval);
mcmc.start();
}
/**
* 继承 CountDownTimer 防范
* <p/>
* 重写 父类的方法 onTick() 、 onFinish()
*/
public class MyCountDownTimer extends CountDownTimer {
private String awardee;
/**
* @param millisInFuture 表示以毫秒为单位 倒计时的总数
* <p/>
* 例如 millisInFuture=1000 表示1秒
* @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
* <p/>
* 例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
*/
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
/**
* 处理时间倒计时进行页面刷新
*
* @param millisUntilFinished
*/
@Override
public void onTick(long millisUntilFinished) {
int ss = 1000;
int mi = ss * 60;
long minute = millisUntilFinished / mi;
long second = (millisUntilFinished - minute * mi) / ss;
long milliSecond = millisUntilFinished - minute * mi - second * ss;
String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
String strSecond = second < 10 ? "0" + second : "" + second;//秒
String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
strMilliSecond = milliSecond > 100 ? strMilliSecond.substring(0, strMilliSecond.length() - 1) : "" + strMilliSecond;
tv_status.setText(strMinute + " : " + strSecond + ":" + strMilliSecond);
}
@Override
public void onFinish() {
tv_status.setText("正在计算....");
/* new Handler().postDelayed(new Runnable() {
@Override
public void run() {
tv_status.setVisibility(View.GONE);
//中奖者布局
*//* ll_result.setVisibility(View.VISIBLE);
//中奖者
tv_awardee.setText(awardee);*//*
}
}, 5000);*/
}
/**
* 设置获奖人
*
* @param awardee
*/
public void setAwardee(String awardee) {
this.awardee = awardee;
}
}
}
这篇关于以分,秒,分秒形式的倒计时组合控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!