简单的自定义倒计时按钮

简单的倒计时自定义控件

(注:做项目过程中少不了发送验证码的功能,以前写的时候我都是直接写在Activity里面的,将逻辑代码和业务代码混在了一起,实在是low的不行);最近做了个简单的自定义倒计时控件,是对TextView的简单封装,实现了将业务和逻辑分开,并对外提供了简单设置点击前后背景变化的接口)
效果图如下:



代码比较简单:

<span style="font-size:14px;">public class CountDownButton extends TextView {
    private int mTotolTime = 60;
    private int mInterval = 1;
    private int mNormalColor=0xffffaacc;//默认正常颜色
    private int mDownColor=0xff999999;//按下的颜色
    private OnClickListener mClickListener;
    int temp;
    private CountDownTimer mCountDownTimer;

    public CountDownButton(Context context) {
        super(context);
        init();
    }

    public CountDownButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CountDownButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setClickable(false);
                startCountDown();

//                从外部实现接口,可以处理点击事件等,要处理的逻辑,比如点击时调用发送验证码的接口;倒计时,读秒不可点击等事件控件本身已经实现。
                if(mClickListener!=null){
                    mClickListener.onClick(v);
                }
            }
        });
    }

    public void setClickListener(OnClickListener listener){
        mClickListener=listener;
    }

    //设置总时长
    public void setTotolTime(int totolTime) {
        mTotolTime = totolTime;
        setText(mTotolTime+"秒后重新发送");
        temp=totolTime;
    }
//   设置时间间隔
    public void setInvertal(int invertal) {
        mInterval = invertal;
    }

    public void setNormalColor(int normalColor){
        mNormalColor=normalColor;
    }

    public void setDownColor(int downColor){
        mDownColor=downColor;
    }

    private void startCountDown() {

        temp=mTotolTime;
        if (mCountDownTimer == null) {
            mCountDownTimer = new CountDownTimer(mTotolTime*1000, mInterval*1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    setText(--temp+"秒后重新发送");
                    setBackgroundColor(mDownColor);
                }

                @Override
                public void onFinish() {
                    setText("重新发送");
                    setClickable(true);
                    setBackgroundColor(mNormalColor);
                }
            };
        }
            mCountDownTimer.start();
    }

}</span>


下面是源代码:

http://download.csdn.net/detail/themelove/9422257






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值