android倒计时控件示例
本文为大家分享了android倒计时控件,供大家参考,具体代码如下
/* *Copyright(C)2012The*Project *Allrightreserved. *Version1.002012-2-11 *Authorveally@foxmail.com */ packagecom.ly.sxh.view; importandroid.content.Context; importandroid.database.ContentObserver; importandroid.os.Handler; importandroid.os.SystemClock; importandroid.provider.Settings; importandroid.util.AttributeSet; importandroid.widget.DigitalClock; importjava.util.Calendar; /** *Customdigitalclock *倒计时控件 * *@author */ publicclassCustomDigitalClockextendsDigitalClock{ CalendarmCalendar; privatefinalstaticStringm12="h:mmaa"; privatefinalstaticStringm24="k:mm"; privateFormatChangeObservermFormatChangeObserver; privateRunnablemTicker; privateHandlermHandler; privatelongendTime; privateClockListenermClockListener; privatebooleanmTickerStopped=false; @SuppressWarnings("unused") privateStringmFormat; publicCustomDigitalClock(Contextcontext){ super(context); initClock(context); } publicCustomDigitalClock(Contextcontext,AttributeSetattrs){ super(context,attrs); initClock(context); } privatevoidinitClock(Contextcontext){ if(mCalendar==null){ mCalendar=Calendar.getInstance(); } mFormatChangeObserver=newFormatChangeObserver(); getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI,true,mFormatChangeObserver); setFormat(); } @Override protectedvoidonAttachedToWindow(){ mTickerStopped=false; super.onAttachedToWindow(); mHandler=newHandler(); /** *requestsatickonthenexthard-secondboundary */ mTicker=newRunnable(){ publicvoidrun(){ if(mTickerStopped) return; longcurrentTime=System.currentTimeMillis(); if(currentTime/1000==endTime/1000-5*60){ mClockListener.remainFiveMinutes(); } longdistanceTime=endTime-currentTime; distanceTime/=1000; if(distanceTime==0){ setText("00:00:00"); onDetachedFromWindow(); mClockListener.timeEnd(); }elseif(distanceTime<0){ setText("00:00:00"); }else{ setText(dealTime(distanceTime)); } invalidate(); longnow=SystemClock.uptimeMillis(); longnext=now+(1000-now%1000); mHandler.postAtTime(mTicker,next); } }; mTicker.run(); } /** *dealtimestring * *@paramtime *@return */ publicstaticStringdealTime(longtime){ StringBufferreturnString=newStringBuffer(); longday=time/(24*60*60); longhours=(time%(24*60*60))/(60*60); longminutes=((time%(24*60*60))%(60*60))/60; longsecond=((time%(24*60*60))%(60*60))%60; StringdayStr=String.valueOf(day); StringhoursStr=timeStrFormat(String.valueOf(hours)); StringminutesStr=timeStrFormat(String.valueOf(minutes)); StringsecondStr=timeStrFormat(String.valueOf(second)); returnString.append(hoursStr).append(":").append(minutesStr).append(":").append(secondStr); returnreturnString.toString(); } /** *formattime * *@paramtimeStr *@return */ privatestaticStringtimeStrFormat(StringtimeStr){ switch(timeStr.length()){ case1: timeStr="0"+timeStr; break; } returntimeStr; } @Override protectedvoidonDetachedFromWindow(){ super.onDetachedFromWindow(); mTickerStopped=true; } /** *Clockendtimefromnowon. * *@paramendTime */ publicvoidsetEndTime(longendTime){ this.endTime=endTime; } /** *Pulls12/24modefromsystemsettings */ privatebooleanget24HourMode(){ returnandroid.text.format.DateFormat.is24HourFormat(getContext()); } privatevoidsetFormat(){ if(get24HourMode()){ mFormat=m24; }else{ mFormat=m12; } } privateclassFormatChangeObserverextendsContentObserver{ publicFormatChangeObserver(){ super(newHandler()); } @Override publicvoidonChange(booleanselfChange){ setFormat(); } } publicvoidsetClockListener(ClockListenerclockListener){ this.mClockListener=clockListener; } publicinterfaceClockListener{ voidtimeEnd(); voidremainFiveMinutes(); } }
希望本文所述对大家学习Android软件编程有所帮助。