本文實例為大家分享了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
|
package com.dylan.frame.ui; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import java.util.Timer; import java.util.TimerTask; /** * 自定義倒計時按鈕 * <p/> * * @author Dylan * <p/> * [佛祖保佑 永無BUG] * Created by Dylan on 2015/11/5 0005. */ public class CountdownButton extends Button implements View.OnClickListener { private long lenght = 60 * 1000 ; //默認倒計時時間; private long time; //倒計時時長 private Timer timer; //開始執行倒計時 private TimerTask timerTask; //每次倒計時執行的任務 private String beforeText = "點擊獲取驗證碼" ; private String afterText = "秒后重新獲取" ; private OnClickListener onClickListener; //按鈕點擊事件 /** * 更新顯示的文本 */ private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super .handleMessage(msg); CountdownButton. this .setText(time / 1000 + afterText); time -= 1000 ; if (time < 0 ) { CountdownButton. this .setEnabled( true ); CountdownButton. this .setText(beforeText); clearTimer(); } } }; public CountdownButton(Context context) { super (context); this .setText(beforeText); setOnClickListener( this ); } public CountdownButton(Context context, AttributeSet attrs) { super (context, attrs); setOnClickListener( this ); } public CountdownButton(Context context, AttributeSet attrs, int defStyleAttr) { super (context, attrs, defStyleAttr); setOnClickListener( this ); } /** * 清除倒計時 */ private void clearTimer() { if (timerTask != null ) { timerTask.cancel(); timerTask = null ; } if (timer != null ) { timer.cancel(); timer = null ; } } /** * 設置倒計時時長 * * @param lenght 默認毫秒 */ public void setLenght( long lenght) { this .lenght = lenght; } /** * 設置未點擊時顯示的文字 * * @param beforeText */ public void setBeforeText(String beforeText) { this .beforeText = beforeText; } /** * 設置未點擊后顯示的文字 * * @param beforeText */ public void setAfterText(String afterText) { this .afterText = afterText; } /** * 點擊按鈕后的操作 * * @param v */ @Override public void onClick(View v) { if (onClickListener != null ) { onClickListener.onClick(v); } initTimer(); this .setText(time / 1000 + afterText); this .setEnabled( false ); timer.schedule(timerTask, 0 , 1000 ); } /** * 初始化時間 */ private void initTimer() { time = lenght; timer = new Timer(); timerTask = new TimerTask() { @Override public void run() { handler.sendEmptyMessage( 1 ); } }; } /** * 設置監聽按鈕點擊事件 * * @param onclickListener */ @Override public void setOnClickListener(OnClickListener onclickListener) { if (onclickListener instanceof CountdownButton) { super .setOnClickListener(onclickListener); } else { this .onClickListener = onclickListener; } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/linglongxin24/article/details/49665355