欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android獲取驗(yàn)證碼倒計(jì)時(shí)顯示效果

 更新時(shí)間:2016年10月12日 10:16:45   作者:南塵  
這篇文章主要為大家詳細(xì)介紹了Android獲取驗(yàn)證碼顯示的兩種簡(jiǎn)單實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前面為大家講過(guò)計(jì)時(shí)器的順時(shí)針的兩種方法,在錄制視頻等操作中頗有使用,今天就給大家?guī)?lái)倒計(jì)時(shí)實(shí)現(xiàn)的兩種方式。

雖然最近寫(xiě)的都比較簡(jiǎn)單和基礎(chǔ),不過(guò)簡(jiǎn)單不代表熟悉,基礎(chǔ)不代表就會(huì),大牛繞過(guò),哈,中牛小牛也可以繞過(guò),這個(gè)是寫(xiě)給初學(xué)者的。

先搞個(gè)效果圖。

代碼實(shí)現(xiàn)方式也超級(jí)簡(jiǎn)單啦,這里首推第一種實(shí)現(xiàn)方式,而且也是比較適合大家的,就是通過(guò)直接繼承CountDownTimer來(lái)實(shí)現(xiàn)。

對(duì)于CountDownTimer這個(gè)類(lèi)很簡(jiǎn)單,繼承它的時(shí)候必須重寫(xiě)構(gòu)造方法和實(shí)現(xiàn)其虛擬方法。

構(gòu)造方法的兩個(gè)參數(shù)分別是(倒計(jì)時(shí)開(kāi)始時(shí)間,間隔時(shí)間)

另外兩個(gè)方法分別是onTick(現(xiàn)在還剩的時(shí)間),計(jì)時(shí)結(jié)束后你想做的時(shí)間可以在onFinish()中做。

值的注意的是,所有的時(shí)間都是以毫秒形式來(lái)做的,所以在你使用的時(shí)候要記得整除1000取商。

不過(guò)由于我使用的是私有內(nèi)部類(lèi)的方式對(duì)外部類(lèi)存在引用,為了防止內(nèi)存泄漏,在Activity銷(xiāo)毀的時(shí)候應(yīng)該注意對(duì)其置空,同樣我們也應(yīng)該避免重復(fù)創(chuàng)建對(duì)象。

另外一種方式還是使用我們常用的Handler + Thread的方式來(lái)實(shí)現(xiàn)。不過(guò)實(shí)現(xiàn)的時(shí)候同樣要非常小心內(nèi)存泄漏,因?yàn)槿绻脩?hù)在銷(xiāo)毀Activity的時(shí)候應(yīng)該注意讓其計(jì)時(shí)子線程不再循環(huán),這個(gè)可以通過(guò)設(shè)置一個(gè)tag標(biāo)簽對(duì)其判斷。

這樣在銷(xiāo)毀的時(shí)候把這個(gè)tag標(biāo)簽置為false,結(jié)束線程的執(zhí)行!

下面是實(shí)現(xiàn)代碼:

package com.example.nanchen.timerdemo;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

 private Button mBtnGetCode;
 private TimeCount mTimeCount;
 private Button mBtnGetCode2;
 private boolean timeFlag = true;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 mBtnGetCode = (Button) findViewById(R.id.main_btn_get_code);
 mBtnGetCode.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
  mTimeCount = null;
  mTimeCount = new TimeCount(60 * 1000, 1000);
  mTimeCount.start();
  }
 });

 mBtnGetCode2 = (Button) findViewById(R.id.main_btn_get_code_2);
 mBtnGetCode2.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
  mBtnGetCode2.setClickable(false);
  mBtnGetCode2.setBackgroundColor(getResources().getColor(R.color.btn_unable));
  timeFlag = true;
  new Thread() {
   @Override
   public void run() {
   super.run();
   for (int i = 59; i >= 0 && timeFlag; i--) {
    try {
    sleep(1000);
    Message msg = Message.obtain();
    msg.what = i;
    mHandler.sendMessage(msg);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
   }
   }
  }.start();
  }
 });
 }

 private Handler mHandler = new Handler() {
 @Override
 public void handleMessage(Message msg) {
  super.handleMessage(msg);
  if (msg.what > 0) {
  mBtnGetCode2.setText("(" + msg.what + ")秒后重試");
  } else {
  mBtnGetCode2.setText("獲取驗(yàn)證碼");
  mBtnGetCode2.setClickable(true);
  mBtnGetCode2.setBackgroundColor(getResources().getColor(R.color.colorAccent));
  }
 }


 };


 /**
 * Activity 銷(xiāo)毀的時(shí)候注意把所有引用置為空,防止內(nèi)存泄漏
 */
 @Override
 protected void onDestroy() {
 super.onDestroy();
 mTimeCount = null;
 timeFlag = false;
 }

 /**
 * 實(shí)現(xiàn)倒計(jì)時(shí)的類(lèi)
 */
 private class TimeCount extends CountDownTimer {

 /**
  * @param millisInFuture The number of millis in the future from the call
  *    to {@link #start()} until the countdown is done and {@link #onFinish()}
  *    is called.
  * @param countDownInterval The interval along the way to receive
  *    {@link #onTick(long)} callbacks.
  */
 public TimeCount(long millisInFuture, long countDownInterval) {
  super(millisInFuture, countDownInterval);
 }

 /**
  * 計(jì)時(shí)過(guò)程顯示 按鈕不可用 設(shè)置為灰色
  *
  * @param millisUntilFinished
  */
 @Override
 public void onTick(long millisUntilFinished) {
  mBtnGetCode.setClickable(false);
  mBtnGetCode.setBackgroundColor(getResources().getColor(R.color.btn_unable));
  mBtnGetCode.setText("(" + millisUntilFinished / 1000 + ")秒后重試");
 }

 /**
  * 計(jì)時(shí)結(jié)束調(diào)用
  */
 @Override
 public void onFinish() {
  mBtnGetCode.setClickable(true);
  mBtnGetCode.setText("獲取驗(yàn)證碼方式1");
  mBtnGetCode.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
 }
 }


}

簡(jiǎn)單看一下xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.example.nanchen.timerdemo.MainActivity">

 <Button
 android:layout_marginTop="10dp"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/main_btn_get_code"
 android:text="獲取驗(yàn)證碼方式1"
 android:background="@color/colorPrimaryDark"/>

 <TextView
 android:layout_width="match_parent"
 android:layout_height="1dp"
 android:id="@+id/main_line1"
 android:background="@color/btn_unable"
 android:layout_below="@+id/main_btn_get_code"
 android:layout_marginTop="10dp"/>

 <Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/main_line1"
 android:layout_marginTop="10dp"
 android:text="獲取驗(yàn)證碼方式2"
 android:id="@+id/main_btn_get_code_2"
 android:background="@color/colorAccent"/>

</RelativeLayout>

寫(xiě)在最后:雖然代碼和實(shí)現(xiàn)都非常簡(jiǎn)單,你可能不費(fèi)吹灰之力,不過(guò)倘若轉(zhuǎn)載的話,還是留個(gè)本文鏈接吧~thank you!

github鏈接:https://github.com/nanchen2251/TimerDemo

 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義webView頭部進(jìn)度加載效果

    Android自定義webView頭部進(jìn)度加載效果

    這篇文章主要介紹了Android自定義webView頭部進(jìn)度加載效果,小編畫(huà)一條進(jìn)度線,然后加載webview上面,具體實(shí)現(xiàn)代碼大家參考下本文
    2017-11-11
  • Flutter Navigator路由傳參的實(shí)現(xiàn)

    Flutter Navigator路由傳參的實(shí)現(xiàn)

    本文主要介紹了Flutter Navigator路由傳參的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android8.1 通過(guò)黑名單屏蔽系統(tǒng)短信和來(lái)電功能

    Android8.1 通過(guò)黑名單屏蔽系統(tǒng)短信和來(lái)電功能

    最近小編接到一個(gè)新的需求,需要將8.1 設(shè)備的來(lái)電功能和短信功能都屏蔽掉,特殊產(chǎn)品就是特殊定制。接下來(lái)通過(guò)本文給大家介紹Android8.1 通過(guò)黑名單屏蔽系統(tǒng)短信和來(lái)電功能,需要的朋友參考下吧
    2019-05-05
  • Android開(kāi)發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音

    Android開(kāi)發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音

    這篇文章給大家介紹Android開(kāi)發(fā)四大組件之實(shí)現(xiàn)電話攔截和電話錄音,涉及到android四大基本組件在程序中的應(yīng)用,對(duì)android四大基本組件感興趣的朋友可以參考下本篇文章
    2015-10-10
  • Kotlin StateFlow單數(shù)據(jù)更新熱流設(shè)計(jì)與使用介紹

    Kotlin StateFlow單數(shù)據(jù)更新熱流設(shè)計(jì)與使用介紹

    StateFlow當(dāng)值發(fā)生變化,就會(huì)將值發(fā)送出去,下流就可以接收到新值。在某些場(chǎng)景下,StateFlow比LiveData更適用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-09-09
  • Android布局優(yōu)化之ViewStub控件

    Android布局優(yōu)化之ViewStub控件

    ViewStub是一個(gè)非常輕量級(jí)的View,這篇文章主要為大家詳細(xì)介紹了Android布局優(yōu)化之ViewStub控件的使用方法以及注意事項(xiàng),感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法

    Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)計(jì)算兩個(gè)日期之間天數(shù)并打印所有日期的方法,涉及Android日期時(shí)間相關(guān)轉(zhuǎn)換與運(yùn)算操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android創(chuàng)建一個(gè)Activity的方法分析

    Android創(chuàng)建一個(gè)Activity的方法分析

    這篇文章主要介紹了Android創(chuàng)建一個(gè)Activity的方法,結(jié)合實(shí)例形式分析了Android創(chuàng)建Activity的具體步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2016-04-04
  • Android模擬器中窗口截圖存成文件實(shí)現(xiàn)思路及代碼

    Android模擬器中窗口截圖存成文件實(shí)現(xiàn)思路及代碼

    Android模擬器內(nèi)容是用OpenGL渲染的,所以用一般的編程截圖(如PrintWindow()等)會(huì)是黑屏。這是因?yàn)楫?huà)的東西放在framebuffer里 接下來(lái)介紹如何實(shí)現(xiàn)Android模擬器中窗口截圖存成文件,感興趣的朋友可以了解下哦
    2013-01-01
  • Android與H5交互產(chǎn)生Script Error踩坑解決

    Android與H5交互產(chǎn)生Script Error踩坑解決

    這篇文章主要為大家介紹了Android與H5交互產(chǎn)生Script Error問(wèn)題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08

最新評(píng)論