Android笔记:发送验证码后倒计时

用到手机短信验证码的这个功能,没有用第三方平台,验证码是把手机号提交到后台,后台返回给短信验证码,
先看效果图:
未发送状态:
这里写图片描述
发送完只有60s倒计时:
这里写图片描述
主要的思路是利用了Handler作为一个计时器,在Activity 里面开启一个线程,每隔1000毫秒,更新一下UI界面(**s后重新发送)
接下来贴代码吧:
布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_find_psd"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.dfwy.cxy.edb.ui.FindPsd">
    <!--企业注册-->
    <TextView
        android:id="@+id/tv_regist_title"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="找回密码"
        android:textSize="18sp"
        android:gravity="center"
        android:textColor="@color/colorWhite"
        android:background="@color/colorBlue"
        />
    <!--返回-->
    <ImageView
        android:id="@+id/iv_findPsd_back"
        android:layout_width="15dp"
        android:layout_height="16sp"
        android:src="@mipmap/back"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:adjustViewBounds="false" />
    <!--灰色分割线-->
    <ImageView
        android:id="@+id/iv_findLin1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/colorGray"
        android:layout_below="@+id/tv_regist_title"
        />
    <ScrollView
        android:layout_below="@+id/iv_findLin1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <RelativeLayout
        android:layout_below="@+id/iv_findLin1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!--用户名-->
    <LinearLayout
        android:id="@+id/ll_findPsd_user"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="20dp">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/tv_findPsd_user"
            android:text="用户名"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:textSize="14sp"
            />
        <EditText
            android:id="@+id/edt_findPsd_user"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:editable="false"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:textColor="@color/colorGray"
            android:background="@color/colorHyalin"
            android:textSize="16sp"
            />
    </LinearLayout>
    <!--灰色的分割线-->
    <ImageView
        android:id="@+id/iv_find_lin2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="-10dp"
        android:background="@color/colorGray"
        android:layout_below="@+id/ll_findPsd_user"/>
    <!--灰色的分割线-->
    <ImageView
        android:id="@+id/iv_find_lin3"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="-10dp"
        android:background="@color/colorGray"
        android:layout_below="@+id/ll_findPsd_code"/>

    <!--登录密码-->
    <LinearLayout
        android:id="@+id/ll_findPsd_psd"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/ll_findPsd_code"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/tv_findPsd_psd"
            android:text="密码"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:textSize="14sp"
            />

        <EditText
            android:id="@+id/edt_findPsd_psd"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:hint="请输入6-18位字符"
            android:textColor="@color/colorGray"
            android:background="@color/colorHyalin"
            android:textSize="14sp"
            />
    </LinearLayout>
    <!--灰色的分割线-->
    <ImageView
        android:id="@+id/iv_find_lin4"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="-10dp"
        android:background="@color/colorGray"
        android:layout_below="@+id/ll_findPsd_psd"/>
    <!--短信验证码-->
    <LinearLayout
        android:id="@+id/ll_findPsd_code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="30dp"
        android:layout_below="@+id/iv_find_lin2"
       >
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1.1"
            android:layout_height="wrap_content"
            android:id="@+id/tv_findPsd_code"
            android:text="短信验证码"
            android:layout_centerVertical="true"
            android:layout_marginLeft="40dp"
            android:textSize="14sp"
            />
        <EditText
            android:id="@+id/edt_findPsd_code"
            android:layout_width="0dp"
            android:layout_weight="1.7"
            android:layout_height="wrap_content"
            android:hint="请输入短信验证码"
            android:textColor="@color/colorGray"
            android:background="@color/colorHyalin"
            android:textSize="14sp"
            />
        <!--竖线-->
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.02"
            android:background="@color/colorGray"
            android:layout_marginBottom="15dp"
            />
        <TextView
            android:id="@+id/tv_findPsd_getCode"
            android:layout_width="0dp"
            android:layout_weight="1.5"
            android:layout_height="wrap_content"
            android:text="获取验证码"
            android:paddingLeft="5dp"
            android:layout_marginBottom="15dp"
            android:textColor="@color/colorBlue"
            />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_findPsd_psd2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/iv_find_lin4"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="20dp">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/tv_findPsd_psd2"
            android:text="确认密码"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:textSize="14sp"
            />
        <EditText
            android:id="@+id/edt_findPsd_psd2"
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:hint="请输入6-18位字符"
            android:textColor="@color/colorGray"
            android:background="@color/colorHyalin"
            android:textSize="14sp"
            />
    </LinearLayout>
    <!--灰色的分割线-->
    <ImageView
        android:id="@+id/iv_find_lin5"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="-10dp"
        android:background="@color/colorGray"
        android:layout_below="@+id/ll_findPsd_psd2"/>
    <Button
        android:id="@+id/btn_findSure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape"
        android:layout_below="@+id/iv_find_lin5"
        android:layout_marginTop="40dp"
        android:layout_marginRight="80dp"
        android:layout_marginLeft="80dp"
        android:text="确认"
        android:textColor="@color/colorWhite"
        />
    </RelativeLayout>
    </ScrollView>
</RelativeLayout>

Activity:

package com.dfwy.cxy.edb.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dfwy.cxy.edb.R;
import com.dfwy.cxy.edb.utils.Tactivity;

public class FindPsd extends Tactivity implements View.OnClickListener{
    //返回
    private ImageView iv_back;
    //获取验证码
    private TextView tv_getCode;
    //输入框 用户名,短信验证码,登录密码,确认密码
    private EditText edt_user,edt_smsCode,edt_psd1,edt_psd2;
    //确认
    private Button btn_sure;
    //倒计时
    private int times = 60;
    private Handler handler = new Handler();
    private MyRunnable runnable = new MyRunnable();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_psd);
        initView();
    }

    private void initView() {
        iv_back = $(R.id.iv_findPsd_back);
        tv_getCode = $(R.id.tv_findPsd_getCode);
        edt_user  = $(R.id.edt_findPsd_user);
        edt_psd1 = $(R.id.edt_findPsd_psd);
        edt_psd2 = $(R.id.edt_findPsd_psd2);
        edt_smsCode = $(R.id.edt_findPsd_code);
        btn_sure = $(R.id.btn_findSure);
        iv_back.setOnClickListener(this);
        tv_getCode.setOnClickListener(this);
        btn_sure.setOnClickListener(this);
    }
    //倒计时
    class MyRunnable implements Runnable{
        @Override
        public void run() {
            times--;
            tv_getCode.setText(times+"s后重新发送");
            if (times==0) {
                tv_getCode.setText("重新发送");
                times=60;
                tv_getCode.setClickable(true);
                tv_getCode.setTextColor(Color.BLUE);
            }else {
                tv_getCode.setClickable(false);
                handler.postDelayed(runnable, 1000);
            }
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_findPsd_back:
                finish();
                break;
            case R.id.tv_findPsd_getCode:
                times = 60;
                handler.postDelayed(runnable,1000);
                tv_getCode.setTextColor(Color.GRAY);
                break;
            case R.id.btn_findSure:
                startActivity(new Intent(this,LoginActivity.class));
                finish();
                break;
        }
    }
}

最后还有一个Tactivity 的代码:
这个主要是为了节省输入findViewById.
有兴趣的可以看一下这个文章
Android笔记:再见findViewById
也贴出来吧:


import android.app.Activity;
import android.view.View;

/**
 * Created by cxy on 2016/11/14.
 */

public class Tactivity extends Activity {
    public < T extends View> T $ (int id){
        return (T)findViewById(id);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值