Android账号注册实现点击获取验证码倒计时效果

所属分类: 软件编程 / Android 阅读数: 1212
收藏 0 赞 0 分享

网站中为了防止恶意获取验证短信、验证邮箱,都会在点击获取验证码的按钮上做个倒计时的效果,如何实现这个效果,具体内容如下

效果图:

代码:

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 studio点击跳转WebView详解

这篇文章主要为大家详细介绍了Android studio点击跳转WebView的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义Drawable实现圆形和圆角

这篇文章主要为大家详细介绍了Android自定义Drawable实现圆形和圆角,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义水平渐变进度条

这篇文章主要为大家详细介绍了Android自定义水平渐变进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ToolBar中menu无法同时显示图标和文字问题的解决方法

这篇文章主要为大家详细介绍了ToolBar中menu无法同时显示图标和文字问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解React Native监听Android回退按键与程序化退出应用

这篇文章主要介绍了详解React Native监听Android回退按键与程序化退出应用的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

android实现上传本地图片到网络功能

这篇文章主要为大家详细介绍了android实现上传本地图片到网络功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现QQ登录功能

这篇文章主要为大家详细介绍了Android实现QQ登录功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简单的城市列表功能

这篇文章主要为大家详细介绍了Android实现简单的城市列表功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android Animation之TranslateAnimation(平移动画)

这篇文章主要为大家详细介绍了Animation之TranslateAnimation平移动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 中Failed to read key from keystore解决办法

这篇文章主要介绍了Android 中Failed to read key from keystore解决办法的相关资料,希望通过本能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多