您的位置:首页 > 移动开发 > Android开发

Android 获取验证码

2015-09-08 09:47 375 查看
定义一个类,调用即可,直接看代码。很简单

package com.apicloud.A6970406947389.utils;

import android.os.CountDownTimer;
import android.widget.Button;

import com.apicloud.A6970406947389.R;

/**
* 时间倒计时
*/
public class TimerCount extends CountDownTimer {
private Button bnt;

public TimerCount(long millisInFuture, long countDownInterval, Button bnt) {
super(millisInFuture, countDownInterval);
this.bnt = bnt;
}

public TimerCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub
bnt.setClickable(true);
bnt.setBackgroundResource(R.drawable.bt_bg);//设置为正常色
bnt.setText("获取验证码");
}

@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
bnt.setClickable(false);
bnt.setBackgroundResource(R.drawable.shape_forgetpwd_bg);//设置为灰色
bnt.setText(arg0 / 1000 + "秒重新获取");
//        bnt.setText("还剩"+arg0 / 1000 + "秒");
}
}


在需要的地方直接调用

private TimerCount timer;

timer = new TimerCount(60000,1000,bnt);
getVerifyCode();//执行发送验证码
timer.start();//启动
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: