欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现点击获取验证码60秒后重新获取功能

时间:2021-06-11 08:11:30|栏目:Android代码|点击:

本文实例为大家分享了Android实现点击获取验证码60秒后重新获取的具体代码,供大家参考,具体内容如下

上代码

/**
 * Created by Xia_焱 on 2017/5/7.
 */

public class CountDownTimerUtils extends CountDownTimer {
  private TextView mTextView;

  /**
   * @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 CountDownTimerUtils(TextView textView, long millisInFuture, long countDownInterval) {
    super(millisInFuture, countDownInterval);
    this.mTextView = textView;
  }

  @Override
  public void onTick(long millisUntilFinished) {
    mTextView.setClickable(false); //设置不可点击
    mTextView.setText(millisUntilFinished / 1000 + "秒后可重新发送"); //设置倒计时时间
    mTextView.setBackgroundResource(R.drawable.bg_identify_code_press); //设置按钮为灰
    SpannableString spannableString = new SpannableString(mTextView.getText().toString());
    ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
    spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
    mTextView.setText(spannableString);
  }

  @Override
  public void onFinish() {
    mTextView.setText("重新获取验证码");
    mTextView.setClickable(true);//重新获得点击
    mTextView.setBackgroundResource(R.drawable.bg_identify_code_normal);
  }
}

TextView背景色

bg_identify_code_press

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#C0C0C0" /> <!--填充色 透明-->
  <corners android:radius="7dp" /> <!-- 圆角 -->
</shape>

bg_identify_code_normal

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#2BAF2B" /> <!--填充色 透明-->
  <corners android:radius="7dp" /> <!-- 圆角 -->
</shape>

布局代码

 <TextView
    android:id="@+id/tv_yzm"
    android:layout_width="match_parent"
    android:layout_marginTop="50dp"
    android:layout_height="45dp"
    android:background="@drawable/bg_identify_code_normal"
    android:gravity="center"
    android:text="点击获取验证码"
    android:textColor="#FFF"
    android:textSize="15dp" />

项目代码

 private void initView() {
    tv_yzm = (TextView) findViewById(R.id.tv_yzm);
    tv_yzm.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tv_yzm, 60000, 1000);
        mCountDownTimerUtils.start();
      }
    });
  }

效果图

这里写图片描述

上一篇:Android项目开发之UI设计器

栏    目:Android代码

下一篇:Andorid 日历控件库,可左右滑动,显示公历,农历,节假日等功能

本文标题:Android实现点击获取验证码60秒后重新获取功能

本文地址:http://www.codeinn.net/misctech/139952.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有