Android实现获取验证码效果

功能非常简单就是定义一个CountDownTimer直接看代码

首先在XML里面放个按钮代码如下:


[html]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btn_getcode"  
  9.         android:background="#4DB74A"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentLeft="true"  
  13.         android:layout_alignParentRight="true"  
  14.         android:layout_centerVertical="true"  
  15.         android:text="点我获取验证码" />  
  16.   
  17. </RelativeLayout>  
下面就是主代码:

[java]  view plain copy
  1. package com.example.countdowntimer;  
  2.   
  3.   
  4.   
  5. import android.os.Bundle;  
  6. import android.os.CountDownTimer;  
  7. import android.app.Activity;  
  8. import android.graphics.Color;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13.   
  14. public class MainActivity extends Activity {  
  15.     private TimeCount time;  
  16.     private Button btnGetcode;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         time = new TimeCount(600001000);  
  22.         btnGetcode=(Button) findViewById(R.id.btn_getcode);  
  23.         btnGetcode.setOnClickListener(new OnClickListener() {  
  24.               
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 time.start();  
  28.             }  
  29.         });  
  30.     }  
  31.     class TimeCount extends CountDownTimer {  
  32.   
  33.         public TimeCount(long millisInFuture, long countDownInterval) {  
  34.             super(millisInFuture, countDownInterval);  
  35.         }  
  36.   
  37.         @Override  
  38.         public void onTick(long millisUntilFinished) {  
  39.             btnGetcode.setBackgroundColor(Color.parseColor("#B6B6D8"));  
  40.             btnGetcode.setClickable(false);  
  41.             btnGetcode.setText(millisUntilFinished / 1000 + "秒后可重新发送");  
  42.         }  
  43.   
  44.         @Override  
  45.         public void onFinish() {  
  46.             btnGetcode.setText("重新获取验证码");  
  47.             btnGetcode.setClickable(true);  
  48.             btnGetcode.setBackgroundColor(Color.parseColor("#4EB84A"));  
  49.   
  50.         }  
  51.     }  
  52.   
  53. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值