首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 数码设备 >

CountDownDigitalClock:记时的TextView

2012-08-08 
CountDownDigitalClock:倒计时的TextViewhttp://www.cnblogs.com/over140/archive/2010/08/27/1809873.htm

CountDownDigitalClock:倒计时的TextView
http://www.cnblogs.com/over140/archive/2010/08/27/1809873.html

改了一下,但是输出格式未能实现自定义,原因在于下面的代码中显示时间差不正确,我不知道什么原因。

mFormat = "距离结束还有dd天kk小时mm分ss秒";//yyyy-MM-dd hh:mm:ss
mCalendar.setTimeInMillis(mTimeDistance);//为什么这样计算的时间不对???
setText(DateFormat.format(mFormat, mCalendar));



我只能退一步,将就着了,源码是这样的:

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.format.DateFormat;import android.util.AttributeSet;import android.util.Log;public class CountDownDigitalClock extends android.widget.DigitalClock {Calendar mCalendar;private final static String m12 = "h:mm aa";// h:mm:ss aaprivate final static String m24 = "k:mm";// k:mm:ssprivate FormatChangeObserver mFormatChangeObserver;private Runnable mTicker;private Handler mHandler;private boolean mTickerStopped = false;String mFormat;private long mDeadTime;private OnCountDownListener onCountDownListener;public CountDownDigitalClock(Context context) {super(context);initClock(context);}public CountDownDigitalClock(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();}@Overrideprotected 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 mCurrentTime = System.currentTimeMillis();if (mCurrentTime >= mDeadTime) {if (onCountDownListener != null){onCountDownListener.onFinish();}return;}long mTimeDistance = mDeadTime - mCurrentTime;long between = mTimeDistance / 1000;// 转换成秒long day = between / (24 * 3600);long hour = between % (24 * 3600) / 3600;long minute = between % (24 * 3600) % 3600 / 60;long second = between % (24 * 3600) % 3600 % 60;String deadTimeStr = "距离结束还有" + day + "天" + hour + "小时"+ minute + "分" + second + "秒";setText(deadTimeStr);// mFormat = "距离结束还有dd天kk小时mm分ss秒";//yyyy-MM-dd hh:mm:ss// mCalendar.setTimeInMillis(mTimeDistance);//为什么这样计算的时间不对??? // setText(DateFormat.format(mFormat, mCalendar));if (onCountDownListener != null)onCountDownListener.onTick();invalidate();long now = SystemClock.uptimeMillis();long next = now + (1000 - now % 1000);mHandler.postAtTime(mTicker, next);}};mTicker.run();}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();mTickerStopped = true;}/** * 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());}@Overridepublic void onChange(boolean selfChange) {setFormat();}}/** * set the dead time *  * @param deadtime */public void setDeadTime(long deadTime) {this.mDeadTime = deadTime;}public interface OnCountDownListener {public void onFinish();public void onTick();}public void setOnCountDownListener(OnCountDownListener onCountDownListener) {this.onCountDownListener = onCountDownListener;}}


用法:
mClock = (CountDownDigitalClock) findViewById(R.id.myClock);        mClock.setDeadTime(getDeadTimeFromServer());        mClock.setOnCountDownListener(new CountDownDigitalClock.OnCountDownListener() {@Overridepublic void onFinish() {// TODO Auto-generated method stubshowToast("倒计时结束!!!");}@Overridepublic void onTick() {// TODO Auto-generated method stubLog.i("tag", "执行了"+(count++)+"次");}});private long getDeadTimeFromServer(){    Calendar mCalendar = Calendar.getInstance();    mCalendar.set(2012, 5-1, 18);//月份从0开始    mCalendar.set(Calendar.HOUR_OF_DAY, 13);//下午1点    mCalendar.set(Calendar.MINUTE, 0);    mCalendar.set(Calendar.SECOND, 0);    return mCalendar.getTimeInMillis();}

热点排行