Android账号注册实现点击获取验证码倒计时效果
作者:Gxs丶小宇 时间:2023-05-18 05:46:33
网站中为了防止恶意获取验证短信、验证邮箱,都会在点击获取验证码的按钮上做个倒计时的效果,如何实现这个效果,具体内容如下
效果图:
代码:
RegisterActivity.java
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import com.jialianjia.bzw.BaseActivity;
import com.jialianjia.bzw.R;
import com.jialianjia.bzw.utils.CountDownButtonHelper;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
/**
* 注册功能
* Created by GXS on 2016/4/21.
*/
public class RegisterActivity extends BaseActivity{
private Button btn_yzm;
@ViewInject(R.id.toolbar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ViewUtils.inject(this);
initToolBar(toolbar,"账号注册",true);
// 获取验证码--start
btn_yzm = (Button) findViewById(R.id.btn_yzm);
btn_yzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CountDownButtonHelper helper = new CountDownButtonHelper(btn_yzm,"倒计时",60,1);
helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {
@Override
public void finish() {
// Toast.makeText(RegisterActivity.this,"倒计时结束",Toast.LENGTH_SHORT).show();
btn_yzm.setText("再次获取");
}
});
helper.start();
}
}); // 获取验证码--end
}
}
布局文件:
activity_register.xml
<?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:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"
android:fitsSystemWindows="true" />
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_phone"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/tv_info_phone"
android:layout_margin="8dp"
android:textColor="@color/black"
android:layout_gravity="center_vertical"
android:textSize="17sp"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="190dp"
android:layout_height="35dp"
android:layout_toRightOf="@id/tv_phone"
android:background="@null"
android:hint="@string/edt_phone_tips"
android:maxLength="20"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:textSize="16sp" />
<View
android:layout_width="2px"
android:layout_height="50dp"
android:background="@color/driverline" />
<Button
android:id="@+id/btn_yzm"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="3dp"
android:layout_gravity="center"
android:background="@color/white"
android:text="@string/btn_yzm_text"/>
</LinearLayout>
<View
android:id="@+id/view_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/tv_phone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/driverline" />
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_yzm"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/tv_yzm_text"
android:layout_margin="8dp"
android:textColor="@color/black"
android:layout_gravity="center_vertical"
android:textSize="17sp"/>
<EditText
android:id="@+id/et_yzm"
android:layout_width="190dp"
android:layout_height="35dp"
android:layout_toRightOf="@id/tv_phone"
android:background="@null"
android:hint="@string/edt_yzm_tips"
android:maxLength="20"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:textSize="16sp" />
</LinearLayout>
<View
android:id="@+id/view_line1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/tv_phone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/driverline" />
<LinearLayout
android:layout_height="50dp"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_password"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="@string/tv_password_text"
android:layout_margin="8dp"
android:textColor="@color/black"
android:layout_gravity="center_vertical"
android:textSize="17sp"/>
<EditText
android:id="@+id/et_password"
android:layout_width="190dp"
android:layout_height="35dp"
android:layout_toRightOf="@id/tv_phone"
android:background="@null"
android:hint="@string/edt_password_text"
android:maxLength="20"
android:layout_gravity="center_vertical"
android:singleLine="true"
android:textSize="16sp" />
</LinearLayout>
<View
android:id="@+id/view_line2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/tv_phone"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/driverline" />
<Button
android:id="@+id/btn_regist"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/btn_select"
android:gravity="center"
android:padding="5dp"
android:text="@string/btn_regist_text"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
标签:Android,验证码,倒计时,注册
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中PriorityQueue实现最小堆和最大堆的用法
2022-03-25 14:32:18
![](https://img.aspxhome.com/file/2023/8/62758_0s.png)
IDEA 2022 中的Lombok 使用基础教程
2023-04-09 21:57:09
![](https://img.aspxhome.com/file/2023/7/64547_0s.png)
C#使用Enum.TryParse()实现枚举安全转换
2021-10-27 05:29:06
![](https://img.aspxhome.com/file/2023/5/119405_0s.png)
C# 三种序列化方法分享
2022-09-21 03:43:05
Netty分布式高性能工具类recycler的使用及创建
2022-03-04 17:57:32
![](https://img.aspxhome.com/file/2023/4/87204_0s.png)
Android分屏多窗口的实践代码
2021-06-21 21:34:58
Mybatis自定义TypeHandler解决特殊类型转换问题详解
2023-04-14 09:50:51
Android利用GridView实现单选效果
2022-08-12 03:31:50
![](https://img.aspxhome.com/file/2023/4/139664_0s.jpg)
详解C# WebApi 接口测试工具:WebApiTestClient
2022-05-15 07:58:44
![](https://img.aspxhome.com/file/2023/7/132327_0s.png)
Android 触摸事件监听(Activity层,ViewGroup层,View层)详细介绍
2023-02-07 00:58:51
Android launcher中模拟按home键的实现
2023-03-25 02:33:48
android中SwipeRefresh实现各种上拉,下拉刷新示例
2022-06-14 17:18:03
![](https://img.aspxhome.com/file/2023/6/138856_0s.png)
Java面向对象之内部类案例讲解
2021-09-16 13:28:19
![](https://img.aspxhome.com/file/2023/9/105969_0s.png)
java对指定目录下文件读写操作介绍
2023-01-26 11:53:17
SpringMVC请求参数的使用总结
2022-11-30 22:23:18
![](https://img.aspxhome.com/file/2023/0/79320_0s.png)
什么是递归?用Java写一个简单的递归程序
2022-02-11 19:39:45
![](https://img.aspxhome.com/file/2023/2/60772_0s.jpg)
Android实现长截屏功能
2022-03-02 15:20:27
教你怎么用SpringBoot+Mybati-Plus快速搭建代码
2023-08-07 12:42:10
![](https://img.aspxhome.com/file/2023/1/58081_0s.png)
Java中的字节流文件读取教程(二)
2023-09-27 04:59:02
如何在Mac下配置多个Java版本
2023-11-07 05:14:34