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

Android验证码倒计时功能实现

2016-03-17 11:04 639 查看
1、首先,自定义一个验证码倒计时类,继承CountDownTimer

/**
* 用于验证码倒计时
* @author Sunday
*
*/
public class MyCountDownTime extends CountDownTimer {

private Button btn;
private String message;

public MyCountDownTime(long millisInFuture, long countDownInterval,
Button btn, String message) {
super(millisInFuture, countDownInterval);
this.btn=btn;
this.message =message;
// TODO Auto-generated constructor stub
}

@Override
public void onFinish() {
// TODO Auto-generated method stub
btn.setEnabled(true);
btn.setText(message);

}

@Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
btn.setEnabled(false);
btn.setText("倒计时:(" + arg0 / 1000 + ")");
}

}
2、具体在代码中的用法如下

public class MainActivity extends BaseActivity implements OnClickListener {

private EditText et_phone, et_smscode;
private Button btn_send_smscode,btn_reset, btn_submit;
private MyCountDownTime myCountDownTime;// 用于验证码倒计时
private Handler mHandler;//用于执行耗时操作

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}

/**
*
* @Description: TODO 初始化控件布局
* @author Sunday
* @date 2016年3月16日
*/
private void initView() {
// TODO Auto-generated method stub
et_phone = (EditText) findViewById(R.id.et_phone);
et_smscode = (EditText) findViewById(R.id.et_smscode);
btn_send_smscode = (Button) findViewById(R.id.btn_send_smscode);
btn_reset = (Button) findViewById(R.id.btn_reset);
btn_submit = (Button) findViewById(R.id.btn_submit);
}

/**
* @Description: TODO 绑定监听事件
* @throws @author
*             Sunday
* @date 2016年3月16日
*/
private void setListener() {
// TODO Auto-generated method stub
btn_send_smscode.setOnClickListener(this);
btn_submit.setOnClickListener(this);
btn_reset.setOnClickListener(this);
}

/*
*点击事件
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_send_smscode:
getCode();
break;
case R.id.btn_submit:
Toasters("提交成功");
break;
case R.id.btn_reset:
cancelTimer();
Toasters("重置成功");
break;
default:
break;
}
}
/**
*
* @Description: TODO 获取验证码,模拟网络访问耗时操作
* @author Sunday
* @date 2016年3月16日
*/
private void getCode(){
buildProgressDialog("获取验证码中,请稍等");
mHandler = new Handler();
mHandler.postDelayed(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub
cancelProgressDialog();
Toasters("获取验证码成功,稍后请查看手机信息");
startTimer();
}
}, 2000);
}

/**
*
* @Description: TODO 发送成功后,开始倒计时
* @throws @author
*             Sunday
* @date 2016年3月16日
*/
private void startTimer() {
if (null == myCountDownTime) {
myCountDownTime = new MyCountDownTime(60000, 1000, btn_send_smscode, "重新发送");
}
myCountDownTime.start();
}
/**
*
* @Description: TODO 一般发送失败时,需要重置Button状态
* @author Sunday
* @date 2016年3月16日
*/
private void cancelTimer(){
if(null != myCountDownTime){
myCountDownTime.cancel();
myCountDownTime.onFinish();
}
}


3、布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="20dp"
android:orientation="horizontal" >

<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="请输入手机号码"
android:inputType="number"
android:maxLength="11"
android:paddingLeft="8dp" />

<Button
android:id="@+id/btn_send_smscode"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:gravity="center"
android:padding="4dp"
android:text="获取验证码"
android:textSize="15sp" >
</Button>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="20dp"
android:orientation="horizontal" >

<EditText
android:id="@+id/et_smscode"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:hint="有时验证码获取失败的时候需要使用"
android:paddingLeft="8dp" />

<Button
android:id="@+id/btn_reset"
android:layout_width="90dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:layout_marginRight="4dp"
android:gravity="center"
android:padding="4dp"
android:text="重置验证码"
android:textSize="15sp" >
</Button>
</LinearLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="提交"
android:textSize="18sp" />
</RelativeLayout>

</LinearLayout>



项目源码地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: