最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

開發(fā)過程中會(huì)遇見很多app注冊時(shí),需要通過手機(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);
  }

  /**
   * 開始計(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)證碼按鈕

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

相關(guān)文章

  • Android LeakCanary的使用方法介紹

    Android LeakCanary的使用方法介紹

    在Android的性能優(yōu)化中,內(nèi)存優(yōu)化是必不可少的點(diǎn),而內(nèi)存優(yōu)化最重要的一點(diǎn)就是解決內(nèi)存泄漏的問題,在Android的內(nèi)存泄漏分析工具也不少,比如PC端的有:AndroidStudio自帶的Android Profiler、MAT等工具;手機(jī)端也有,就是我們今天要介紹的LeakCanary
    2022-09-09
  • 詳解Android觀察者模式的使用與優(yōu)劣

    詳解Android觀察者模式的使用與優(yōu)劣

    這篇文章主要介紹了Android觀察者模式的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Android的設(shè)計(jì)模式,感興趣的朋友可以了解下
    2020-09-09
  • Android中WebView的使用與后退鍵處理詳細(xì)講解

    Android中WebView的使用與后退鍵處理詳細(xì)講解

    博主自從開始寫安卓以來,一直飽受WebView的摧殘,好在網(wǎng)上一大堆的大神給出了他們成長路上遇到的坑以及一些解決辦法,這篇文章主要給大家介紹了關(guān)于Android中WebView的使用與后退鍵處理的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • Android實(shí)現(xiàn)Activities之間進(jìn)行數(shù)據(jù)傳遞的方法

    Android實(shí)現(xiàn)Activities之間進(jìn)行數(shù)據(jù)傳遞的方法

    這篇文章主要介紹了Android實(shí)現(xiàn)Activities之間進(jìn)行數(shù)據(jù)傳遞的方法,涉及Android中Activities的使用技巧,需要的朋友可以參考下
    2015-04-04
  • Android實(shí)現(xiàn)文字上下滾動(dòng)效果

    Android實(shí)現(xiàn)文字上下滾動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文字上下滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • kotlin實(shí)現(xiàn)快遞與號碼歸屬地查詢案例詳解

    kotlin實(shí)現(xiàn)快遞與號碼歸屬地查詢案例詳解

    時(shí)間軸時(shí)一個(gè)很炫酷的效果,一般作用在物流信息上,我們同樣也可以作為一個(gè)學(xué)習(xí)對象去學(xué)習(xí)他的使用方法,同時(shí)呢,我們可以在線查詢我們的電話號碼歸屬地,巧用鍵盤的邏輯提升我們用戶體驗(yàn)
    2023-02-02
  • Android本地?cái)?shù)據(jù)存儲(chǔ)Room實(shí)踐和優(yōu)化技巧

    Android本地?cái)?shù)據(jù)存儲(chǔ)Room實(shí)踐和優(yōu)化技巧

    本文詳細(xì)介紹了Android本地?cái)?shù)據(jù)存儲(chǔ)框架Room的使用,包括基本概念、核心組件、最佳實(shí)踐、優(yōu)化技巧等,幫助開發(fā)者學(xué)習(xí)和掌握Room的使用方法,提升數(shù)據(jù)存儲(chǔ)效率和應(yīng)用性能
    2023-04-04
  • Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單

    Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單

    這篇文章主要為大家詳細(xì)介紹了Android使用CoordinatorLayout實(shí)現(xiàn)底部彈出菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android編程設(shè)置屏幕亮度的方法

    Android編程設(shè)置屏幕亮度的方法

    這篇文章主要介紹了Android編程設(shè)置屏幕亮度的方法,結(jié)合實(shí)例形式分析了Android獲取及設(shè)置屏幕亮度的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • Android布局之TableLayout表格布局

    Android布局之TableLayout表格布局

    Tablelayout類以行和列的形式對控件進(jìn)行管理,每一行為一個(gè)TableRow對象,或一個(gè)View控件。當(dāng)為TableRow對象時(shí),可在TableRow下添加子控件,默認(rèn)情況下,每個(gè)子控件占據(jù)一列。 當(dāng)為View時(shí),該View將獨(dú)占一行
    2015-12-12

最新評論

渑池县| 双鸭山市| 永吉县| 甘德县| 洪泽县| 安仁县| 黄梅县| 苍山县| 胶南市| 汤阴县| 如皋市| 岳西县| 花垣县| 城口县| 尼木县| 旬邑县| 高安市| 衢州市| 奉化市| 林甸县| 承德市| 九寨沟县| 紫金县| 额济纳旗| 隆化县| 惠州市| 准格尔旗| 民和| 阳山县| 同德县| 兴业县| 绥德县| 巴东县| 顺昌县| 奈曼旗| 梅州市| 措勤县| 漳平市| 南丹县| 朝阳区| 綦江县|