欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现倒计时功能代码

程序员文章站 2023-11-12 09:57:22
效果图,每隔1秒,变换一下时间  xml: 复制代码 代码如下: 效果图,每隔1秒,变换一下时间
android实现倒计时功能代码 
xml:
复制代码 代码如下:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".mainactivity"
android:background="#000000"
>
<com.gettime.customdigitalclock
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:textcolor="#ff0000"
android:textsize="45sp" />
</relativelayout>
//上面 <com.gettime.customdigitalclock 为类的完整位置
mainactivity.java
package com.gettime;
import android.os.bundle;
import android.app.activity;
public class mainactivity extends activity {
private customdigitalclock timeclock;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
timeclock=(customdigitalclock) findviewbyid(r.id.time);
timeclock.setendtime(system.currenttimemillis()+30*1000*24*24);
}
}
customdigitalclock.java
/*
* copyright (c) 2012 the * project
* all right reserved.
* version 1.00 2012-2-11
* author veally@foxmail.com
*/
package com.gettime;
import java.util.calendar;
import android.content.context;
import android.database.contentobserver;
import android.os.handler;
import android.os.systemclock;
import android.provider.settings;
import android.text.html;
import android.text.spannable;
import android.text.spanned;
import android.text.style.absolutesizespan;
import android.util.attributeset;
import android.widget.digitalclock;
/**
* custom digital clock
*
* @author veally@foxmail.com
*/
public class customdigitalclock extends digitalclock {
calendar mcalendar;
private final static string m12 = "h:mm aa";
private final static string m24 = "k:mm";
private formatchangeobserver mformatchangeobserver;
private runnable mticker;
private handler mhandler;
private long endtime;
public static long distancetime;
private clocklistener mclocklistener;
private static boolean isfirst;
private boolean mtickerstopped;
@suppresswarnings("unused")
private string mformat;
public customdigitalclock(context context) {
super(context);
initclock(context);
}
public customdigitalclock(context context, attributeset attrs) {
super(context, attrs);
initclock(context);
}
private void initclock(context context) {
if (mcalendar == null) {
mcalendar = calendar.getinstance();
}
mformatchangeobserver = new formatchangeobserver();
getcontext().getcontentresolver().registercontentobserver(
settings.system.content_uri, true, mformatchangeobserver);
setformat();
}
@override
protected void onattachedtowindow() {
mtickerstopped = false;
super.onattachedtowindow();
mhandler = new handler();
/**
* requests a tick on the next hard-second boundary
*/
mticker = new runnable() {
public void run() {
if (mtickerstopped)
return;
long currenttime = system.currenttimemillis();
if (currenttime / 1000 == endtime / 1000 - 5 * 60) {
mclocklistener.remainfiveminutes();
}
distancetime = endtime - currenttime;
distancetime /= 1000;
if (distancetime == 0) {
settext("00:00:00");
ondetachedfromwindow();
} else if (distancetime < 0) {
settext("00:00:00");
ondetachedfromwindow();
} else {
settext(dealtime(distancetime));
}
invalidate();
long now = systemclock.uptimemillis();
long next = now + (1000 - now % 1000);
mhandler.postattime(mticker, next);
}
};
mticker.run();
}
/**
* deal time string
*
* @param time
* @return
*/
public static spanned dealtime(long time) {
spanned str;
stringbuffer returnstring = new stringbuffer();
long day = time / (24 * 60 * 60);
long hours = (time % (24 * 60 * 60)) / (60 * 60);
long minutes = ((time % (24 * 60 * 60)) % (60 * 60)) / 60;
long second = ((time % (24 * 60 * 60)) % (60 * 60)) % 60;
string daystr = string.valueof(day);
string hoursstr = timestrformat(string.valueof(hours));
string minutesstr = timestrformat(string.valueof(minutes));
string secondstr = timestrformat(string.valueof(second));
returnstring.append(daystr).append("天").append(hoursstr).append("小时")
.append(minutesstr).append("分钟").append(secondstr).append("秒");
str = html.fromhtml(returnstring.tostring());
if (day >= 10) {
((spannable) str).setspan(new absolutesizespan(16), 2, 3,
spannable.span_exclusive_exclusive);
((spannable) str).setspan(new absolutesizespan(16), 5, 7,
spannable.span_exclusive_exclusive);
((spannable) str).setspan(new absolutesizespan(16), 9, 11,
spannable.span_exclusive_exclusive);
((spannable) str).setspan(new absolutesizespan(16), 13, 14,
spannable.span_exclusive_exclusive);
} else {
((spannable) str).setspan(new absolutesizespan(16), 1, 2,
spannable.span_exclusive_exclusive);
((spannable) str).setspan(new absolutesizespan(16), 4, 6,
spannable.span_exclusive_exclusive);
((spannable) str).setspan(new absolutesizespan(16), 8, 10,
spannable.span_exclusive_exclusive);
((spannable) str).setspan(new absolutesizespan(16), 12, 13,
spannable.span_exclusive_exclusive);
}
// return returnstring.tostring();
return str;
}
/**
* format time
*
* @param timestr
* @return
*/
private static string timestrformat(string timestr) {
switch (timestr.length()) {
case 1:
timestr = "0" + timestr;
break;
}
return timestr;
}
@override
protected void ondetachedfromwindow() {
super.ondetachedfromwindow();
mtickerstopped = true;
}
/**
* clock end time from now on.
*
* @param endtime
*/
public void setendtime(long endtime) {
this.endtime = endtime;
}
/**
* pulls 12/24 mode from system settings
*/
private boolean get24hourmode() {
return android.text.format.dateformat.is24hourformat(getcontext());
}
private void setformat() {
if (get24hourmode()) {
mformat = m24;
} else {
mformat = m12;
}
}
private class formatchangeobserver extends contentobserver {
public formatchangeobserver() {
super(new handler());
}
@override
public void onchange(boolean selfchange) {
setformat();
}
}
public void setclocklistener(clocklistener clocklistener) {
this.mclocklistener = clocklistener;
}
public interface clocklistener {
void timeend();
void remainfiveminutes();
}
}