android:点击获取验证码,倒计时60秒

17 篇文章 0 订阅
    private CountDownTimer mCountDownTimer;
    private void requestSendSms() {

        if (mCountDownTimer != null) {
            return;
        } else {
            mCountDownTimer = new CountDownTimer(60 * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    tv_verification_code.setText((millisUntilFinished / 1001) + " s");
                    tv_verification_code.setTextColor(Color.parseColor("#B5B5B5"));
                }

                @Override
                public void onFinish() {
                    mCountDownTimer = null;
                    tv_verification_code.setText("重新获取验证码");
                    tv_verification_code.setTextColor(Color.parseColor("#1E90FF"));
                }
            };
        }
        mCountDownTimer.start();}

调用

/*
* 获取验证码
* */
tv_verification_code.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        /*
         * 请求接口获取验证码
         * */
        requestSendSms();
        PostRequest(edt_phone_number.getText().toString());
    }
});

另外一种就是检测网络的倒计时

 private CountDownTimer mCountDownTimer;
    private void RequestServer() {

        if (mCountDownTimer != null) {
            return;
        } else {
            mCountDownTimer = new CountDownTimer(15 * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    int temp = (int) (millisUntilFinished / 1001);
                    int tmp=15;
                    if(tmp!=temp & temp%1==0){//判断有没有结束,判断是不是整数
                        tmp = temp;
                        Message message = new Message();
                        message.what = 2;
                        handler.sendMessage(message);
                    }
                }

                @Override
                public void onFinish() {
                    mCountDownTimer = null;
                }
            };
        }
        mCountDownTimer.start();}

关闭定时器

mCountDownTimer.cancel();
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,需要在Android Studio中创建一个新的项目,然后在布局文件中创建一个按钮,用于获取验证码。在按钮的onClick方法中,调用后台服务来实现计时功能。 具体步骤如下: 1. 创建一个新项目,并在activity_main.xml文件中添加以下代码来创建一个按钮: ``` <Button android:id="@+id/btn_get_code" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取验证码" /> ``` 2. 在MainActivity中,获取按钮的引用并设置onClick监听器: ``` Button btnGetCode = findViewById(R.id.btn_get_code); btnGetCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO: 调用后台服务实现计时功能 } }); ``` 3. 创建一个后台服务,用于实现计时功能。在该服务中,使用Handler.postDelayed方法来实现计时功能,并在每次计时结束时更新按钮上的文字: ``` public class CountdownService extends Service { private int mCountdownTime = 60; private Handler mHandler = new Handler(); @Override public int onStartCommand(Intent intent, int flags, int startId) { mHandler.postDelayed(mCountdownRunnable, 1000); return super.onStartCommand(intent, flags, startId); } private Runnable mCountdownRunnable = new Runnable() { @Override public void run() { if (mCountdownTime > 0) { mCountdownTime--; mHandler.postDelayed(this, 1000); } else { stopSelf(); } updateButton(); } }; private void updateButton() { String text = mCountdownTime + "s后重新获取"; Intent intent = new Intent("update_button_text"); intent.putExtra("text", text); sendBroadcast(intent); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 4. 在MainActivity中注册一个广播接收器,用于接收服务发送的更新按钮文本的广播,并在接收到广播时更新按钮上的文本: ``` private BroadcastReceiver mUpdateButtonReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String text = intent.getStringExtra("text"); Button btnGetCode = findViewById(R.id.btn_get_code); btnGetCode.setText(text); } }; @Override protected void onResume() { super.onResume(); registerReceiver(mUpdateButtonReceiver, new IntentFilter("update_button_text")); } @Override protected void onPause() { super.onPause(); unregisterReceiver(mUpdateButtonReceiver); } ``` 5. 在按钮的onClick方法中启动服务,并在服务启动后开始计时: ``` Button btnGetCode = findViewById(R.id.btn_get_code); btnGetCode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, CountdownService.class); startService(intent); } }); ``` 6. 在AndroidManifest.xml文件中添加以下代码,以声明后台服务: ``` <service android:name=".CountdownService" /> ``` 这样,当点获取验证码按钮时,后台服务将会启动并开始计时。在计时过程中,服务将会发送广播通知MainActivity更新按钮上的文本。当计时结束时,服务将会自动停止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wenlong Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值