Android倒计时Button

最近做用户绑定,需要用到倒计时的一个Button,就花点时间封装了一个,非常简单,效果图如下:

Android倒计时Button

1.TimeButton 自定义倒计时Button

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

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就完了。

上一篇:1114 Family Property (25 分)


下一篇:使用Android-PullToRefresh实现下拉刷新功能