android 用CountDownTimer实现一个倒计时

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mattdong0106/article/details/41985047
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

猜你喜欢

转载自blog.csdn.net/mattdong0106/article/details/41985047