public class MainActivity extends Activity {
private MyCount mCount;
private Button btn_getVc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_getVc = (Button) findViewById(R.id.reg_getvc_btn);
mCount = new MyCount(180000, 1000);
mCount.start();
}
/*** 省略部分代码... */
/** * 定义一个倒计时的内部类 */
class MyCount extends CountDownTimer {
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
btn_getVc.setText("获取验证码");
btn_getVc.setEnabled(true);
}
@Override
public void onTick(long millisUntilFinished) {
btn_getVc.setText("重新获取(" + millisUntilFinished / 1000 + "秒)");
btn_getVc.setEnabled(false);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mCount.cancel();
}
}
方法中文说明:http://www.cnblogs.com/over140/archive/2011/12/20/2294220.html