Android验证码倒计时功能实现
16lz
2021-01-23
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}@Overridepublic void onFinish() {// TODO Auto-generated method stubbtn.setEnabled(true);btn.setText(message);}@Overridepublic void onTick(long arg0) {// TODO Auto-generated method stubbtn.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;//用于执行耗时操作@Overrideprotected 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 stubet_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 stubbtn_send_smscode.setOnClickListener(this);btn_submit.setOnClickListener(this);btn_reset.setOnClickListener(this);}/* *点击事件 */@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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() {@Overridepublic void run() {// TODO Auto-generated method stubcancelProgressDialog();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>
项目源码地址
更多相关文章
- 【转】Android获取用于操作数据库的SQLiteDatabase实例【学习记
- 【Android开发小记--9】触摸事件---实现双指缩放图片
- Android inject input events 注入Touch 点(x, y) 触摸输入事件
- Android自定义dialog弹出布局
- Android Sqlite数据库查询操作使用 '%?%' 的问题
- LinearLayout 线性布局
随机推荐
- [RK3399][Android7.1.1] WifiAp:开机默认
- 在Fragment中设置控件点击方法,执行失败。
- use ffmpeg to setup streaming server o
- Android系统启动流程(一)解析init进程启动
- Handler.post(action)方法
- [Android NDK]Android JNI开发例子 ---3
- Android studio3.x 多渠道打包apk
- Android Chromium WebView html js 开发
- android(NDK+JNI)---Eclipse+CDT+gdb调试
- 关于Android Studio3.2新建项目Android r