一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Android - Android獲取驗證碼倒計時實現代碼

Android獲取驗證碼倒計時實現代碼

2022-03-10 15:14FBY展菲 Android

這篇文章主要為大家詳細介紹了Android獲取驗證碼倒計時的實現代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android獲取驗證碼倒計時的具體代碼,供大家參考,具體內容如下

1. 驗證碼輸入框和獲取驗證碼按鈕布局

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
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="50dp"
   android:background="@color/white"
   android:orientation="horizontal" >
 
   <EditText
     android:id="@+id/phonetext"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:layout_marginLeft="15dp"
     android:layout_gravity="center_vertical"
     android:inputType="number"
     android:hint="請輸入短信驗證碼"
     android:background="@null"/>
 
   <Button
     android:id="@+id/timebutton"
     android:layout_width="wrap_content"
     android:layout_height="30dp"
     android:layout_marginRight="15dp"
     android:layout_marginTop="10dp"
     android:textSize="16dp"
     android:background="@drawable/tv_timemessage_bg"
     android:text="獲取"
    />
</LinearLayout>

效果如下:

Android獲取驗證碼倒計時實現代碼

2. 根據id設置Button點擊事件觸發倒計時

JAVA代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * Created by fby on 2017/9/11.
 */
public class ChargepsdActivity extends Activity {
 
  private Button timeButton;
 
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chargepsd);
 
    timeButton = (Button) findViewById(R.id.timebutton);
    //new倒計時對象,總共的時間,每隔多少秒更新一次時間
    final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);
 
    //設置Button點擊事件觸發倒計時
    timeButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        myCountDownTimer.start();
      }
    });
}

3. 倒計時函數

?
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
//倒計時函數
  private class MyCountDownTimer extends CountDownTimer {
 
    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }
 
    //計時過程
    @Override
    public void onTick(long l) {
      //防止計時過程中重復點擊
      timeButton.setClickable(false);
      timeButton.setText(l/1000+"秒");
 
    }
 
    //計時完畢的方法
    @Override
    public void onFinish() {
      //重新給Button設置文字
      timeButton.setText("重新獲取");
      //設置可點擊
      timeButton.setClickable(true);
    }
  }
 
}

4. 清除倒計時函數,解決驗證碼輸入正確后停止計時

?
1
2
3
4
5
6
7
8
9
10
private void clearTimer() {
    if (task != null) {
      task.cancel();
      task = null;
    }
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_36478920/article/details/78326305

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产美女做爰免费视频软件 | 日日骑夜夜骑 | 日本大尺度动漫在线观看缘之空 | 青青草国产一区二区三区 | 国产精品欧美亚洲韩国日本99 | 国产欧美日韩在线播放 | 欧美肥胖老妇做爰变态 | 99久久精品久久久久久清纯 | 欧美激情影音先锋 | 精品综合久久久久久88小说 | 国产成人精品一区二三区在线观看 | 欧美精品一二三区 | 青青青手机在线观看 | 成人在线视频观看 | 青青草在视线频久久 | 99精品国产综合久久久久 | 国内自拍视频在线观看 | nxgx在线观看国产中文 | 77色视频在线 | 我把校花黑色蕾丝胸罩脱了 | 男女男精品视频免费观看 | 九九免费高清在线观看视频 | 男人使劲躁女人小视频 | 国产亚洲精品日韩香蕉网 | 免费观看在线永久免费xx视频 | 天天综合天天综合 | 乌克兰精品摘花处破 | 911色_911色sss在线观看 | 成人精品免费网站 | 国产亚洲精品线观看77 | 暖暖免费观看高清在线 | 成人毛片1024你懂的 | 97福利社| 97精品国产自在现线免费观看 | 日本黄色大片免费观看 | 日韩毛片免费线上观看 | 黄 色 大 片 网站 | 91啦在线播放 | 91成人免费视频 | a级免费观看 | 大香线一本 |