Android 验证码倒计时功能的实现

来源:互联网 发布:手机音乐后期软件 编辑:程序博客网 时间:2024/05/16 08:50

最近一直在做公司内Android开发,前两天公司扫描出漏洞,需要给登录首页添加验证码验证的功能。看了看网上的感觉有点乱,就自己记录了一下,多了不说了,直接看代码吧:

//获取验证码security_code_btn = (Button) this.findViewById(R.id.security_code_btn);security_code_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {TimeCount helper = new TimeCount(ZWLoginActivity.this,60000,security_code_btn);helper.start();// 验证通过,下面这只是我们公司点击获取验证码调取的方法,在这你可以添加你自己公司的调取方法JSONObject json = new JSONObject();Map<String, Object> params = null;try {json.put(StaffInfo.USERNAME_NODE, mUsername.getText());params = ParamHelper.buildJSONParam(URLs.SECURITY_CODE_API, json);} catch (JSONException e) {e.printStackTrace();}loginCoadCallback = new JsonAjaxCallback<JSONObject>() {@Overridepublic void callback(String url, JSONObject json, AjaxStatus status) {Log.d("验证码:",json+"");parseCoadResult(url,json,status);}};aQuery.ajax(URLs.SECURITY_CODE_API, params, JSONObject.class,loginCoadCallback);}});
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;}@Overridepublic void onFinish() {// 计时完毕button.setTextColor(context.getResources().getColor(R.color.white));button.setBackgroundResource(R.drawable.zw_button_round_bg);button.setText("再次获取");button.setClickable(true);}@Overridepublic void onTick(long millisUntilFinished) {// 计时过程button.setTextColor(context.getResources().getColor(R.color.white));button.setClickable(false);//防止重复点击button.setBackgroundResource(R.drawable.zw_button_round_bg);button.setText(millisUntilFinished / TIME_TASCK+"秒可重发");}}



原创粉丝点击