Android怎么获取验证码倒计时

这篇文章主要介绍Android怎么获取验证码倒计时,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联主营乐山网站建设的网络公司,主营网站建设方案,手机APP定制开发,乐山h5成都小程序开发搭建,乐山网站营销推广欢迎乐山等地区企业咨询

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>

效果如下:

Android怎么获取验证码倒计时

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是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

以上是“Android怎么获取验证码倒计时”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!

本文题目:Android怎么获取验证码倒计时
URL地址:https://www.cdcxhl.com/article46/jssshg.html

成都网站建设公司_创新互联,为您提供企业网站制作网站营销定制开发软件开发企业建站品牌网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站