一个自定义倒计时button

这是一个倒计时功能的button。

使用的时候,btn对象直接调用start,cancel方法就可以实现开始计时,取消计时

public class CountdownButton extends android.support.v7.widget.AppCompatButton {

    private final int able = R.drawable.plat_condition_btn_able;
    private Stopwatch stopwatch = null;
    private int countTime = 60;

    public CountdownButton(Context context) {
        this(context, null);
    }

    public CountdownButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CountdownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setText(" 点击获取验证码 ");
        resetWatch();
        setEnabled(true);
    }

    @Override
    public void setEnabled(boolean enabled) {
        setBackground(enabled);
        super.setEnabled(enabled);
    }

    private void setBackground(boolean enabled) {
        if (enabled) {
            setTextColor(Color.WHITE);
            setBackgroundResource(able);
        } else {
            setTextColor(0xFFa29395);
            setBackground(null);
        }
    }

    public void start() {
        setEnabled(false);
        stopwatch.cancel();
        stopwatch.start();
    }

    public void cancel() {
        setEnabled(true);
        stopwatch.cancel();
        setText(" 重新获取验证码 ");
        resetWatch();
    }

    private void resetWatch(){
        stopwatch = new Stopwatch(countTime * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                setText(" " + (millisUntilFinished / 1000 + ((millisUntilFinished % 1000 > 500) ? 1 : 0) + "s后重新发送 "));
            }

            @Override
            public void onFinish() {
                setText(" 重新获取验证码 ");
                setEnabled(true);
            }
        };
    }
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        cancel();
    }
}

计时器stopwatch:

public abstract class Stopwatch{
    private static final String TAG = "Stopwatch";
    /**
     * Millis since epoch when alarm should stop.
     */
    private final long mMillisInFuture;

    /**
     * The interval in millis that the user receives callbacks
     */
    private final long mCountdownInterval;

    private long mStopTimeInFuture;

    /**
     * boolean representing if the timer was cancelled
     */
    private boolean mCancelled = false;

    /**
     * @param millisInFuture The number of millis in the future from the call
     *   to {@link #start()} until the countdown is done and {@link #onFinish()}
     *   is called.
     * @param countDownInterval The interval along the way to receive
     *   {@link #onTick(long)} callbacks.
     */
    public Stopwatch(long millisInFuture, long countDownInterval) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }
    /**
     * Cancel the countdown.
     */
    public synchronized final void cancel() {
        mCancelled = true;
        mHandler.removeMessages(MSG);
    }

    /**
     * Start the countdown.
     */
    public synchronized final Stopwatch start() {
        mCancelled = false;
        if (mMillisInFuture <= 0) {
            onFinish();
            return this;
        }
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;
    }


    /**
     * Callback fired on regular interval.
     * @param millisUntilFinished The amount of time until finished.
     */
    public abstract void onTick(long millisUntilFinished);

    /**
     * Callback fired when the time is up.
     */
    public abstract void onFinish();


    private static final int MSG = 1;


    // handles counting down
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            synchronized (Stopwatch.this) {
                if (mCancelled) {
                    return;
                }
                final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
                Log.d(TAG, "Handler"+millisLeft);
                if (millisLeft <= 0) {
                    onFinish();
                } else if (millisLeft < mCountdownInterval) {
                    if(mMillisInFuture%mCountdownInterval==0)
                        onTick(millisLeft);
                    sendMessageDelayed(obtainMessage(MSG), millisLeft);
                } else {
                    long lastTickStart = SystemClock.elapsedRealtime();
                    onTick(millisLeft);

                    // take into account user's onTick taking time to execute
                    long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();

                    // special case: user's onTick took more than interval to
                    // complete, skip to next interval
                    while (delay < 0) delay += mCountdownInterval;
                    sendMessageDelayed(obtainMessage(MSG), delay);
                }
            }
        }
    };
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python的GUI库Tkinter来实现悬浮窗口计时工具。同时,你还需要使用Python的时间模块time来处理计时逻辑,以及使用Python的语音合成库pyttsx3来进行语音提醒。 下面是一个示例代码,实现了你所要求的功能: ```python import tkinter as tk import time import pyttsx3 class CountdownTimer: def __init__(self, root): self.root = root self.root.title("悬浮窗口计时工具") self.countdown_sec = 0 self.remaining_sec = 0 self.timer_running = False self.label_time = tk.Label(self.root, text="00:00:00", font=("Arial", 24)) self.label_time.pack(pady=20) self.entry_time = tk.Entry(self.root) self.entry_time.insert(0, "请输入计时时间(秒)") self.entry_time.pack(pady=10) self.button_start = tk.Button(self.root, text="开始", command=self.start_timer) self.button_start.pack(pady=5) self.button_pause = tk.Button(self.root, text="暂停", state=tk.DISABLED, command=self.pause_timer) self.button_pause.pack(pady=5) self.button_reset = tk.Button(self.root, text="重置", state=tk.DISABLED, command=self.reset_timer) self.button_reset.pack(pady=5) self.button_stop = tk.Button(self.root, text="停止", state=tk.DISABLED, command=self.stop_timer) self.button_stop.pack(pady=5) self.engine = pyttsx3.init() def start_timer(self): if self.timer_running: return try: self.countdown_sec = int(self.entry_time.get()) if self.countdown_sec <= 0: raise ValueError except ValueError: self.show_error_message("请输入一个正整数的计时时间") return self.remaining_sec = self.countdown_sec self.update_time_label() self.button_start.config(state=tk.DISABLED) self.button_pause.config(state=tk.NORMAL) self.button_reset.config(state=tk.NORMAL) self.button_stop.config(state=tk.NORMAL) self.timer_running = True self.timer_tick() def pause_timer(self): if not self.timer_running: return self.timer_running = False self.button_pause.config(text="继续", command=self.resume_timer) def resume_timer(self): if self.timer_running: return self.timer_running = True self.button_pause.config(text="暂停", command=self.pause_timer) self.timer_tick() def reset_timer(self): self.timer_running = False self.remaining_sec = self.countdown_sec self.update_time_label() self.button_start.config(state=tk.NORMAL) self.button_pause.config(state=tk.DISABLED, text="暂停", command=self.pause_timer) self.button_reset.config(state=tk.DISABLED) self.button_stop.config(state=tk.DISABLED) def stop_timer(self): self.timer_running = False self.remaining_sec = 0 self.update_time_label() self.button_start.config(state=tk.NORMAL) self.button_pause.config(state=tk.DISABLED, text="暂停", command=self.pause_timer) self.button_reset.config(state=tk.DISABLED) self.button_stop.config(state=tk.DISABLED) def timer_tick(self): if self.remaining_sec <= 0: self.timer_running = False self.update_time_label() self.play_audio() return self.update_time_label() self.remaining_sec -= 1 if self.timer_running: self.root.after(1000, self.timer_tick) def update_time_label(self): hours = self.remaining_sec // 3600 minutes = (self.remaining_sec % 3600) // 60 seconds = self.remaining_sec % 60 time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds) self.label_time.config(text=time_str) def play_audio(self): self.engine.say("计时结束") self.engine.runAndWait() def show_error_message(self, message): tk.messagebox.showerror("错误", message) root = tk.Tk() app = CountdownTimer(root) root.mainloop() ``` 运行以上代码,将会弹出一个悬浮窗口计时工具。你可以在输入框中输入计时时间(秒),然后点击开始按钮即可开始计时。在计时结束时,会有语音提醒。 请注意,你需要先安装pyttsx3库,可以使用以下命令安装: ```shell pip install pyttsx3 ``` 希望以上代码能够满足你的需求!如有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值