最近做用户绑定,需要用到倒计时的一个Button,就花点时间封装了一个,非常简单,效果图如下:
1.TimeButton 自定义倒计时Button
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
|
package
com.example.timebutton;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
/** * 时间控制的Button
*
* @author xue
* @time 2014-02-26 10:53:55
*
*/
public
class TimeButton extends
Button {
private
int time, tempTime;
private
String subText;
public
final int TIME_START = 0x10 ;
public
final int TIME_REDUCE = 0x11 ;
public
final int TIME_END = 0x12 ;
public
final String GONING_TAG = "TAG_GONGING" ;
public
final String END_TAG = "TAG_END" ;
public
TimeButton(Context context) {
super (context);
// TODO Auto-generated constructor stub
}
public
TimeButton(Context context, AttributeSet attrs) {
super (context, attrs);
// TODO Auto-generated constructor stub
}
public
TimeButton(Context context, AttributeSet attrs, int
defStyle) {
super (context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
private
Handler handler = new
Handler() {
@Override
public
void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch
(msg.what) {
case
TIME_START:
setTag(GONING_TAG);
break ;
case
TIME_REDUCE:
setText( "("
+ tempTime-- + ")"
+ "秒后重新" + subText);
break ;
case
TIME_END:
setTag(END_TAG);
setText(subText);
tempTime = time;
break ;
default :
break ;
}
}
};
/**
* 继承OnclickListener,用于设置Onclick监听器时向上转型
*
* @author xue
*
*/
public
class TimeOnclickListener implements
OnClickListener {
// 用于判断是否在倒计时完成后执行一系列动作
public
boolean END_TAG = true ;
@Override
public
void onClick(View v) {
// TODO Auto-generated method stub
if
(v.getTag() != null
&& v.getTag().equals(GONING_TAG)) {
END_TAG = false ;
return ;
}
setText(tempTime + "" );
new
Thread( new
Runnable() {
@Override
public
void run() {
// TODO Auto-generated method stub
// 开始倒计时
handler.sendEmptyMessage(TIME_START);
while
(tempTime >= 0 ) {
// 正在倒计时
handler.sendEmptyMessage(TIME_REDUCE);
try
{
Thread.sleep( 1000 );
} catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 倒计时结束
END_TAG = true ;
handler.sendEmptyMessage(TIME_END);
}
}).start();
}
}
/**
* 设置倒计时时间
*
* @param time
*/
public
void setTime( int
time) {
this .time = time;
this .tempTime = time;
}
/**
* 设置倒计时过程中的文字
*
* @param subText
*/
public
void setSubText(String subText) {
this .subText = subText;
}
} |
2.MainActivity
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
|
package
com.example.timebutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
/** * 主界面
*
* @author xue
* @time 2014-02-26 15:34:51
*
*/
public
class MainActivity extends
Activity {
private
TimeButton time;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
@Override
public
boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true ;
}
private
void initView() {
time = (TimeButton) findViewById(R.id.timebutton);
time.setTime( 60 ); // 设置倒计时时间
time.setSubText( "发送" );
}
private
void setListener() {
time.setOnClickListener(time. new
TimeOnclickListener() {
@Override
public
void onClick(View v) {
// TODO Auto-generated method stub
super .onClick(v);
if
( this .END_TAG) // 如果不加判断,则每次点击Button都会执行下面这句话
Toast.makeText(MainActivity. this , "已发送!" , 1000 ).show();
}
});
}
} |
OK,一个简单的倒计时Button就完了。