温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-25 19:39:59 来源:脚本之家 阅读:183 作者:Xia_焱 栏目:移动开发

本文实例为大家分享了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实现点击获取验证码60秒后重新获取功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI