栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

安卓实现发送验证码倒计时功能

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

安卓实现发送验证码倒计时功能

一.xml页面

1.编写文本和按钮控件

            
            
二.编写java页面

1.在Activity页面中获取控件id和button的id并设置button的id的点击事件

//获取id
btn_graphVerify = findViewById(R.id.btn_graphVerify);
//button的id的点击事件
btn_phoneVerify.setOnClickListener(new OnClickListener() {
	@Override
	public void onClick(View v) {
        TimeCount helper = new TimeCount(this,60000,btn_phoneVerify);
        helper.start();
        //此方法为我公司点击获取短信验证码接口,这里可以添加自己公司的方法
        getverifyLogin(); 
}

2.新建一个TimeCount帮助类

public class TimeCount extends CountDownTimer {
    private static final int TIME_TASCK = 1000;
    private Button button;
    private Context context;
    public TimeCount(Context context, long millisInFuture, Button view) {
        super(millisInFuture, TIME_TASCK);
        button = view;
        this.context = context;
    }

    @Override
    public void onFinish() {// 计时完毕
        button.setTextColor(context.getResources().getColor(R.color.white));
        button.setBackgroundResource(R.drawable.verify_style);
        button.setText("再次获取");
        button.setClickable(true);
    }

    @Override
    public void onTick(long millisUntilFinished) {// 计时过程
        button.setTextColor(context.getResources().getColor(R.color.white));
        button.setClickable(false);//防止重复点击
        button.setBackgroundResource(R.drawable.unverify_style);
        button.setText(millisUntilFinished / TIME_TASCK+"秒后可重发");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032285.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号