显示倒计时的Button按钮

Posted zqxLonely

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了显示倒计时的Button按钮相关的知识,希望对你有一定的参考价值。

package com.pingyijinren.helloworld.activity;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.pingyijinren.helloworld.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private TimeCount timeCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);

        timeCount=new TimeCount(60000,1000);
    }

    @Override
    public void onClick(View v) {
        timeCount.start();
    }

    private class TimeCount extends CountDownTimer {
        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            button.setEnabled(false);
            button.setText(millisUntilFinished/1000+"秒");
        }

        @Override
        public void onFinish() {
            button.setEnabled(true);
            button.setText("重新获取");
        }
    }
}

 

以上是关于显示倒计时的Button按钮的主要内容,如果未能解决你的问题,请参考以下文章

iOS 短信验证码倒计时按钮的实现

MAC AxureRP9登录获取验证码倒计时

WPF自定义控件之带倒计时的按钮--Button

像Button不正常工作

iOS 短信验证码倒计时按钮的实现

从活动中获取片段内的按钮