激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - android實(shí)現(xiàn)驗(yàn)證碼按鈕

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

2022-03-10 15:13qq_14876133 Android

這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)驗(yàn)證碼按鈕功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

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

attrs.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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

?
1
2
3
4
5
6
<?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>
?
1
2
3
4
5
6
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/qq_14876133/article/details/80913886

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品7区 | 欧美国产一区二区三区激情无套 | 12av毛片| 护士hd老师fre0性xxx | 九九色在线观看 | av成人在线免费观看 | 久久久久久久久久久高潮一区二区 | 久草在线精品观看 | 欧美性xxxx狂欢老少配 | 欧美精品一区二区久久 | 国产精品成aⅴ人片在线观看 | 久久久国产视频 | 福利片在线看 | 一区二区三区视频播放 | 在线看91| 免费在线观看成人av | 成人性生活视频在线观看 | 精品欧美一区二区精品久久久 | 伊人一二三四区 | 久久久久99999 | 久久精品亚洲一区二区 | 欧美第1页 | www日韩大片 | 亚洲第一成av人网站懂色 | lutube成人福利在线观看污 | 亚洲一区二区三区视频 | 中国hdxxxx护士爽在线观看 | 免费观看9x视频网站在线观看 | 少妇色诱麻豆色哟哟 | 美女在线视频一区二区 | 国产亚洲精品久久久久久久久 | 噜噜在线视频 | 日日狠狠久久 | 久久国产91 | 久久国产精品久久久久 | 久久一区二区三区av | 久久久久久久久久久亚洲 | 91av视频大全 | 二区三区偷拍浴室洗澡视频 | 国产扩阴视频 | 久久网页 |