Android自定義DigitalClock控件實(shí)現(xiàn)商品倒計(jì)時(shí)
本文實(shí)例為大家分享了DigitalClock實(shí)現(xiàn)商品倒計(jì)時(shí)的具體代碼,供大家參考,具體內(nèi)容如下
自定義DigitalClock控件:
package com.veally.timesale;
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.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;
private ClockListener mClockListener;
private boolean mTickerStopped = false;
@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();
}
long distanceTime = endTime - currentTime;
distanceTime /= 1000;
if (distanceTime == 0) {
setText("00:00:00");
onDetachedFromWindow();
mClockListener.timeEnd();
} else if (distanceTime < 0) {
setText("00:00:00");
} 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 String dealTime(long time) {
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(hoursStr).append(":").append(minutesStr).append(":").append(secondStr);
return returnString.toString();
}
/**
* 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();
}
}
使用:
remainTime.setEndTime(mItems.get(position).getRemainTime());
remainTime.setClockListener(new CustomDigitalClock.ClockListener() { // register the clock's listener
@Override
public void timeEnd() {
// The clock time is ended.
}
@Override
public void remainFiveMinutes() {
// The clock time is remain five minutes.
}
});
倒計(jì)時(shí)剩余時(shí)間最好從服務(wù)器獲取。

代碼下載:DigitalClock實(shí)現(xiàn)倒計(jì)時(shí)
原創(chuàng)作者:veally@foxmail.com 源鏈接不詳,工作用到就記錄分享下!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android入門之TextClock的使用教程
- Android服務(wù)應(yīng)用ClockService實(shí)現(xiàn)鬧鐘功能
- Android ScreenLockReceiver監(jiān)聽(tīng)鎖屏功能示例
- Android應(yīng)用程序保持后臺(tái)喚醒(使用WakeLock實(shí)現(xiàn))
- Android DigitalClock組件用法實(shí)例
- Android AnalogClock簡(jiǎn)單使用方法實(shí)例
- Android中系統(tǒng)自帶鎖WalkLock與KeyguardLock用法實(shí)例詳解
- Android控件之AnalogClock與DigitalClock用法實(shí)例分析
- Android Lock鎖實(shí)現(xiàn)原理詳細(xì)分析
相關(guān)文章
Android自定義控件之水平圓點(diǎn)加載進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義控件之水平圓點(diǎn)加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
OpenHarmony實(shí)現(xiàn)類Android短信驗(yàn)證碼及倒計(jì)時(shí)流程詳解
這篇文章主要介紹了OpenHarmony實(shí)現(xiàn)類Android短信驗(yàn)證碼及倒計(jì)時(shí)流程,發(fā)送短信驗(yàn)證碼后,一般在界面上都會(huì)有一個(gè)倒計(jì)時(shí)的顯示.在安卓中,實(shí)現(xiàn)類似的倒計(jì)時(shí)有多種方式,當(dāng)然背后的基本原理都是設(shè)定一個(gè)初始值,然后每過(guò)一定的間隔時(shí)間執(zhí)行操作2022-11-11
Android Toast實(shí)現(xiàn)全屏顯示
這篇文章主要為大家詳細(xì)介紹了Android Toast實(shí)現(xiàn)全屏顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android快速實(shí)現(xiàn)無(wú)預(yù)覽拍照功能
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)無(wú)預(yù)覽拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Android 文件存儲(chǔ)及常見(jiàn)問(wèn)題解決
這篇文章主要介紹了Android 文件存儲(chǔ)及常見(jiàn)問(wèn)題解決的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android自定義View實(shí)現(xiàn)掃描效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)掃描效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04

