溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android怎么獲取驗(yàn)證碼倒計(jì)時(shí)

發(fā)布時(shí)間:2021-05-22 13:35:44 來源:億速云 閱讀:145 作者:小新 欄目:移動(dòng)開發(fā)

這篇文章主要介紹Android怎么獲取驗(yàn)證碼倒計(jì)時(shí),文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

Android獲取驗(yàn)證碼倒計(jì)時(shí)的具體代碼,具體內(nèi)容如下

1. 驗(yàn)證碼輸入框和獲取驗(yàn)證碼按鈕布局

xml代碼:

<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="請(qǐng)輸入短信驗(yàn)證碼"
     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怎么獲取驗(yàn)證碼倒計(jì)時(shí)

2. 根據(jù)id設(shè)置Button點(diǎn)擊事件觸發(fā)倒計(jì)時(shí)

JAVA代碼:

/**
 * 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倒計(jì)時(shí)對(duì)象,總共的時(shí)間,每隔多少秒更新一次時(shí)間
    final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);

    //設(shè)置Button點(diǎn)擊事件觸發(fā)倒計(jì)時(shí)
    timeButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        myCountDownTimer.start();
      }
    });
}

3. 倒計(jì)時(shí)函數(shù)

//倒計(jì)時(shí)函數(shù)
  private class MyCountDownTimer extends CountDownTimer {

    public MyCountDownTimer(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }

    //計(jì)時(shí)過程
    @Override
    public void onTick(long l) {
      //防止計(jì)時(shí)過程中重復(fù)點(diǎn)擊
      timeButton.setClickable(false);
      timeButton.setText(l/1000+"秒");

    }

    //計(jì)時(shí)完畢的方法
    @Override
    public void onFinish() {
      //重新給Button設(shè)置文字
      timeButton.setText("重新獲取");
      //設(shè)置可點(diǎn)擊
      timeButton.setClickable(true);
    }
  }

}

4. 清除倒計(jì)時(shí)函數(shù),解決驗(yàn)證碼輸入正確后停止計(jì)時(shí)

private void clearTimer() {
    if (task != null) {
      task.cancel();
      task = null;
    }
    if (timer != null) {
      timer.cancel();
      timer = null;
    }
  }

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

以上是“Android怎么獲取驗(yàn)證碼倒計(jì)時(shí)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI