Android验证码倒计时功能实现
来源:互联网 发布:微信怎么推广淘宝店 编辑:程序博客网 时间:2024/05/16 04:17
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>
项目源码地址
2 0
- Android验证码倒计时功能实现
- Android开发:验证码倒计时功能实现
- Android 验证码倒计时功能的实现
- Android 验证码倒计时功能
- android短信验证码功能实现,包含按钮倒计时操作
- Android实现获取验证码的倒计时功能
- Android实现获取验证码的倒计时功能
- Android实现获取验证码的倒计时功能
- Android实现获取验证码的倒计时功能
- Android获取验证码倒计时功能(CountDownTimer实现)
- Android获取验证码倒计时功能(Timer实现)
- 实现 发送验证码的倒计时功能
- Handler实现验证码获取倒计时功能
- 实现获取验证码倒计时功能
- iOS - 实现验证码倒计时功能
- Ionic Angular 实现验证码倒计时功能
- React Native实现验证码倒计时功能
- android获取验证码倒计时功能
- codeforces 29C Mail Stamps(dfs)
- 百度地图SDK在Android Studio中报错(添加 jniLibs.srcDir 'libs')
- Java获取当前时间的年月日时分秒方法
- Redis 集群
- “埋怨” 和 “抱怨”
- Android验证码倒计时功能实现
- CentOS-6.5-64位下搭建jdk7 + mysql5.5 + tomcat7 + nginx
- 在写一个iOS应用之前必须做的7件事(附相关资源)
- apktool 反编译 遇到文件解决
- Java之美[从菜鸟到高手演变]之设计模式
- QListWidget用法
- Java基础知识——Sting、StringBuild、StringBuffer的区别
- 配置ADT的具体做法
- eclipse 常见问题解决