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

Android獲取驗(yàn)證碼倒計(jì)時(shí)實(shí)現(xiàn)代碼

 更新時(shí)間:2022年08月02日 11:25:13   作者:Swift社區(qū)  
這篇文章主要為大家詳細(xì)介紹了Android獲取驗(yàn)證碼倒計(jì)時(shí)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了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>

效果如下:

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;
    }
  }

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

相關(guān)文章

最新評(píng)論