亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android實(shí)現(xiàn)驗(yàn)證碼按鈕

 更新時(shí)間:2018年07月06日 11:43:33   作者:qq_14876133  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)驗(yàn)證碼按鈕功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

開(kāi)發(fā)過(guò)程中會(huì)遇見(jiàn)很多app注冊(cè)時(shí),需要通過(guò)手機(jī)發(fā)送驗(yàn)證碼驗(yàn)證 ,這是可以封裝一個(gè)驗(yàn)證碼按鈕:

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="VerifyCodeButton">
    <!--默認(rèn)背景-->
    <attr name="android:background" />
    <!--點(diǎn)擊后背景-->
    <attr name="clickedBackground" format="reference" />
    <!--倒計(jì)時(shí)間-->
    <attr name="countdownTime" format="integer" />
    <!--倒計(jì)時(shí)間后提示文字-->
    <attr name="countdownText" format="string" />
  </declare-styleable>
</resources>

自定義Button

public class VerifyCodeButton extends Button {
  private Context mContext;
  private int mClickedBackground;//點(diǎn)擊后背景
  private int mBackground;//當(dāng)前背景
  private String mCountdownownText;
  private int mCountdownTime = 60;
  private TimeCount mTimeCount;

  public VerifyCodeButton(Context context) {
    this(context, null);
  }

  public VerifyCodeButton(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.buttonStyle);
  }

  public VerifyCodeButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
    initAttrs(attrs);
    init();
  }

  private void initAttrs(AttributeSet attrs) {
    TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.VerifyCodeButton);
    mBackground = typedArray.getResourceId(R.styleable.VerifyCodeButton_android_background, mBackground);
    mClickedBackground = typedArray.getResourceId(R.styleable.VerifyCodeButton_clickedBackground, mClickedBackground);
    mCountdownTime = typedArray.getInt(R.styleable.VerifyCodeButton_countdownTime, mCountdownTime);
    mCountdownownText = typedArray.getString(R.styleable.VerifyCodeButton_countdownText);
    typedArray.recycle();
  }

  private void init() {
    setBackgroundResource(mBackground);
    mTimeCount = new TimeCount(mCountdownTime * 1000, 1000);
  }

  /**
   * 開(kāi)始計(jì)時(shí)
   */
  public void start() {
    mTimeCount.start();
  }

  /**
   * 取消計(jì)時(shí)
   */
  public void cancle() {
    mTimeCount.cancel();
    setClickable(true);
    setText(mCountdownownText != null ? mCountdownownText : "");
    setBackgroundResource(mBackground);
  }

  class TimeCount extends CountDownTimer {

    /**
     * @param millisInFuture  總時(shí)間
     * @param countDownInterval 間隔時(shí)間
     */
    public TimeCount(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }

    /**
     * @param millisUntilFinished 當(dāng)前時(shí)間
     */
    @Override
    public void onTick(long millisUntilFinished) {
      setClickable(false);
      setText(String.valueOf(millisUntilFinished / 1000 + "s"));
      setBackgroundResource(mClickedBackground);
    }

    @Override
    public void onFinish() {
      setClickable(true);
      setText(mCountdownownText != null ? mCountdownownText : "");
      setBackgroundResource(mBackground);
    }
  }
}

自定義2個(gè)drawable

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <corners android:radius="5dp" />
  <solid android:color="#feacc3" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle">
  <corners android:radius="5dp" />
  <solid android:color="#999999" />
</shape>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.sample.verify.MainActivity">

  <com.sample.verify.widget.VerifyCodeButton
    android:id="@+id/btn_verify_code"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="10dp"
    android:background="@drawable/bg_btn_default"
    android:gravity="center"
    android:text="獲取驗(yàn)證碼"
    android:textColor="#ffffff"
    android:textSize="14sp"
    app:clickedBackground="@drawable/bg_btn_clicked"
    app:countdownText="重新獲取"
    app:countdownTime="10" />

  <Button
    android:id="@+id/btn_cancle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="取消倒計(jì)時(shí)" />

</LinearLayout>

Activity

package com.sample.verify;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.sample.verify.widget.VerifyCodeButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  private VerifyCodeButton btn_verify_code;
  private Button btn_cancle;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle("驗(yàn)證碼");

    btn_verify_code = (VerifyCodeButton) findViewById(R.id.btn_verify_code);
    btn_cancle = (Button) findViewById(R.id.btn_cancle);

    btn_verify_code.setOnClickListener(this);
    btn_cancle.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.btn_verify_code:
        btn_verify_code.start();
        break;
      case R.id.btn_cancle:
        btn_verify_code.cancle();
        break;
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (btn_verify_code != null) {
      btn_verify_code.cancle();
    }
  }
}

代碼下載:android實(shí)現(xiàn)驗(yàn)證碼按鈕

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

相關(guān)文章

  • Flutter進(jìn)階質(zhì)感設(shè)計(jì)之標(biāo)簽欄

    Flutter進(jìn)階質(zhì)感設(shè)計(jì)之標(biāo)簽欄

    這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階質(zhì)感設(shè)計(jì)之標(biāo)簽欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性)

    Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性)

    這篇文章主要介紹了Android 自定義驗(yàn)證碼輸入框的實(shí)例代碼(支持粘貼連續(xù)性),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 為什么不要在?Flutter?中使用全局變量

    為什么不要在?Flutter?中使用全局變量

    這篇文章主要介紹了為什么不要在Flutter中使用全局變量,全局變量是公共變量,可以被Flutter程序中的每個(gè)方法和對(duì)象訪問(wèn),全局變量是局部變量的替代品,它們?cè)诜椒ㄖ袆?chuàng)建并在該方法中訪問(wèn)
    2022-08-08
  • Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼

    Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼

    利用Android的ViewFlipper和AnimationUtils實(shí)現(xiàn)圖片帶有動(dòng)畫(huà)的輪播切換,其中當(dāng)點(diǎn)擊“上一張”圖片時(shí),切換到上一張圖片;當(dāng)點(diǎn)擊“下一張”圖片時(shí),切換到下一張圖片,本文給大家介紹Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼,需要的朋友參考下
    2015-12-12
  • Android CoordinatorLayout詳解及實(shí)例代碼

    Android CoordinatorLayout詳解及實(shí)例代碼

    這篇文章主要介紹了Android CoordinatorLayout詳解及實(shí)例代碼的相關(guān)資料,CoordinatorLayout基本實(shí)現(xiàn)兩個(gè)功能: 作為頂層布局 和調(diào)度協(xié)調(diào)子布局,這里詳細(xì)介紹此部分知識(shí),需要的朋友可以參考下
    2016-12-12
  • Android獲取App內(nèi)存使用情況的方法

    Android獲取App內(nèi)存使用情況的方法

    本篇文章主要介紹了Android獲取App內(nèi)存使用情況的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • Android沉浸式狀態(tài)欄實(shí)現(xiàn)示例

    Android沉浸式狀態(tài)欄實(shí)現(xiàn)示例

    本篇文章主要介紹了Android沉浸式狀態(tài)欄實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • Android自定義view利用PathEffect實(shí)現(xiàn)動(dòng)態(tài)效果

    Android自定義view利用PathEffect實(shí)現(xiàn)動(dòng)態(tài)效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義view利用PathEffect實(shí)現(xiàn)動(dòng)態(tài)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Android?JobScheduler詳細(xì)介紹

    Android?JobScheduler詳細(xì)介紹

    JobScheduler是Android?5.0引入的系統(tǒng)服務(wù),它可以根據(jù)網(wǎng)絡(luò)狀態(tài)、充電狀態(tài)、電量和存儲(chǔ)狀況等來(lái)觸發(fā)相應(yīng)的JobService執(zhí)行任務(wù),它支持多條件組合、持久性任務(wù),以及在API?21以上版本的Android系統(tǒng)中使用,對(duì)Android?JobScheduler相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • Andriod 獲取電池的信息實(shí)例代碼

    Andriod 獲取電池的信息實(shí)例代碼

    通過(guò)本段實(shí)例代碼給大家介紹Andriod 獲取電池的信息的相關(guān)知識(shí),對(duì)android獲取電池信息相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03

最新評(píng)論