Android實現擷取驗證碼效果

來源:互聯網
上載者:User

標籤:android開發   驗證碼擷取   定時器   

功能非常簡單就是定義一個CountDownTimer直接看代碼

首先在XML裡面放個按鈕代碼如下:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/btn_getcode"        android:background="#4DB74A"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:text="點我擷取驗證碼" /></RelativeLayout>
下面就是主代碼:

package com.example.countdowntimer;import android.os.Bundle;import android.os.CountDownTimer;import android.app.Activity;import android.graphics.Color;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private TimeCount time;private Button btnGetcode;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);time = new TimeCount(60000, 1000);btnGetcode=(Button) findViewById(R.id.btn_getcode);btnGetcode.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {time.start();}});}class TimeCount extends CountDownTimer {public TimeCount(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}@Overridepublic void onTick(long millisUntilFinished) {btnGetcode.setBackgroundColor(Color.parseColor("#B6B6D8"));btnGetcode.setClickable(false);btnGetcode.setText(millisUntilFinished / 1000 + "秒後可重新發送");}@Overridepublic void onFinish() {btnGetcode.setText("重新擷取驗證碼");btnGetcode.setClickable(true);btnGetcode.setBackgroundColor(Color.parseColor("#4EB84A"));}}}
源碼下載:



Android實現擷取驗證碼效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.