您好,登录后才能下订单哦!
密码登录
            
            
            
            
        登录注册
            
            
            
        点击 登录注册 即表示同意《亿速云用户服务条款》
        这篇文章主要介绍Android怎么获取验证码倒计时,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Android获取验证码倒计时的具体代码,具体内容如下
1. 验证码输入框和获取验证码按钮布局
xml代码:
<LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/white" android:orientation="horizontal" > <EditText android:id="@+id/phonetext" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginLeft="15dp" android:layout_gravity="center_vertical" android:inputType="number" android:hint="请输入短信验证码" android:background="@null"/> <Button android:id="@+id/timebutton" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_marginRight="15dp" android:layout_marginTop="10dp" android:textSize="16dp" android:background="@drawable/tv_timemessage_bg" android:text="获取" /> </LinearLayout>
效果如下:

2. 根据id设置Button点击事件触发倒计时
JAVA代码:
/**
 * Created by fby on 2017/9/11.
 */
public class ChargepsdActivity extends Activity {
  private Button timeButton;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chargepsd);
    timeButton = (Button) findViewById(R.id.timebutton);
    //new倒计时对象,总共的时间,每隔多少秒更新一次时间
    final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);
    //设置Button点击事件触发倒计时
    timeButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        myCountDownTimer.start();
      }
    });
}3. 倒计时函数
//倒计时函数
  private class MyCountDownTimer extends CountDownTimer {
    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }
    //计时过程
    @Override
    public void onTick(long l) {
      //防止计时过程中重复点击
      timeButton.setClickable(false);
      timeButton.setText(l/1000+"秒");
    }
    //计时完毕的方法
    @Override
    public void onFinish() {
      //重新给Button设置文字
      timeButton.setText("重新获取");
      //设置可点击
      timeButton.setClickable(true);
    }
  }
}4. 清除倒计时函数,解决验证码输入正确后停止计时
private void clearTimer() {
    if (task != null) {
      task.cancel();
      task = null;
    }
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
以上是“Android怎么获取验证码倒计时”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。