开屏倒计时
我用的是handler和message实现的倒计时操作.
具体代码如下.
//先定义一个时间
int time = 5;
//从线程池里拿出消息
Message msg = Message.obtain();
//设置消息的标识
msg.what = 1;
延时1秒后发送消息.
handler.sendMessageDelayed(msg,1000);
handler中的代码如下.
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
time–;
btn.setText(“跳转”+” “+time);
if (time<=-1){
Toast.makeText(AdvertActivity.this,”倒计时结束”,Toast.LENGTH_SHORT).show();
time = 5;
btn.setText(“跳转”+” “+time);
}else{
Message msg1 = Message.obtain();
msg1.what = 1;
handler.sendMessageDelayed(msg1,1000);
}
break;
}
}
};