国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

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

2019-10-21 21:47:38
字體:
來源:轉載
供稿:網友

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

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

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="請輸入短信驗證碼"     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點擊事件觸發(fā)倒計時

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倒計時對象,總共的時間,每隔多少秒更新一次時間    final MyCountDownTimer myCountDownTimer = new MyCountDownTimer(60000,1000);    //設置Button點擊事件觸發(fā)倒計時    timeButton.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        myCountDownTimer.start();      }    });}

3. 倒計時函數

//倒計時函數  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. 清除倒計時函數,解決驗證碼輸入正確后停止計時

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 万年县| 衡阳县| 简阳市| 仁化县| 大港区| 石台县| 崇礼县| 孟州市| 武冈市| 新竹县| 尚志市| 井研县| 棋牌| 林口县| 晋城| 武胜县| 澄城县| 洛隆县| 平凉市| 霸州市| 稻城县| 柞水县| 板桥市| 永德县| 新沂市| 乌拉特前旗| 西乌珠穆沁旗| 巴彦县| 淮北市| 康马县| 台中市| 阿坝县| 大埔区| 湛江市| 红安县| 东阿县| 千阳县| 西乌珠穆沁旗| 宜君县| 宽甸| 镇坪县|