import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
/**
* 倒计时控件
*/
public class TimerTextView extends TextView implements Runnable{
private Context mContext;
public TimerTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
private long mhour, mmin, msecond;//天,小时,分钟,秒
private boolean run=false; //是否启动了
public void setTimes(long[] times) {
mhour = times[0];
mmin = times[1];
msecond = times[2];
}
/**
* 倒计时计算
*/
private void ComputeTime() {
msecond--;
if (msecond < 0) {
mmin--;
msecond = 59;
if (mmin < 0) {
mmin = 59;
mhour--;
if (mhour < 0) {
// 倒计时结束,一天有24个小时
mhour = 23;
}
}
}
if (mhour == 0 && mmin == 0 && msecond == 0){
Toast.makeText(mContext,"时间到了",Toast.LENGTH_SHORT).show();
run = false;
}
}
public boolean isRun() {
return run;
}
public void beginRun() {
this.run = true;
if (mhour == 0 && mmin == 0 && msecond == 0){
Toast.makeText(mContext,"",Toast.LENGTH_SHORT).show();
return;
}
run();
}
public void stopRun(){
this.run = false;
}
@Override
public void run() {
//标示已经启动
if(run){
ComputeTime();
String strTime=(mhour<10?"0"+mhour:mhour)+":"+ (mmin<10?"0"+mmin:mmin)+":"+(msecond<10?"0"+msecond:msecond);
this.setText(strTime);
postDelayed(this, 1000);
}else {
Log.i("removecallbacks","over");
removeCallbacks(this);
}
}
}
倒计时控件
最新推荐文章于 2022-02-12 01:14:24 发布