服务器之家

服务器之家 > 正文

android倒计时控件示例

时间:2021-06-24 13:31     来源/作者:SRian215

本文为大家分享了android倒计时控件,供大家参考,具体代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2012 The * Project
 * All right reserved.
 * Version 1.00 2012-2-11
 * Author veally@foxmail.com
 */
package com.ly.sxh.view;
 
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;
 
import java.util.Calendar;
 
/**
 * Custom digital clock
 * 倒计时控件
 *
 * @author
 */
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();
  }
 
}

希望本文所述对大家学习Android软件编程有所帮助。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部