亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android自定義DigitalClock控件實現商品倒計時

發布時間:2020-09-20 09:17:07 來源:腳本之家 閱讀:144 作者:一葉飄舟 欄目:移動開發

本文實例為大家分享了DigitalClock實現商品倒計時的具體代碼,供大家參考,具體內容如下

自定義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.
 }
 });

倒計時剩余時間最好從服務器獲取。

Android自定義DigitalClock控件實現商品倒計時

代碼下載:DigitalClock實現倒計時

原創作者:veally@foxmail.com 源鏈接不詳,工作用到就記錄分享下!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

沙雅县| 宁南县| 邓州市| 孟州市| 贵港市| 柳州市| 乌兰察布市| 汤阴县| 庆元县| 广平县| 射洪县| 航空| 宜章县| 乳源| 青阳县| 来宾市| 同仁县| 高邑县| 祁门县| 日土县| 景洪市| 彩票| 应用必备| 报价| 白水县| 米林县| 凤庆县| 岑溪市| 光山县| 辉南县| 阿巴嘎旗| 新河县| 广元市| 福安市| 霍城县| 合山市| 灵石县| 大竹县| 汉沽区| 宜城市| 淳化县|