在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 百戰(zhàn)經(jīng)典第七戰(zhàn)-顯示倒計(jì)時(shí)的Button按鈕
百戰(zhàn)經(jīng)典第二十戰(zhàn)-ListView中點(diǎn)擊button跳轉(zhuǎn)到撥號(hào)界面實(shí)例
百戰(zhàn)經(jīng)典第十一戰(zhàn)-GridView動(dòng)態(tài)添加Item
百戰(zhàn)經(jīng)典第二戰(zhàn)-好玩的Spinner控件
百戰(zhàn)經(jīng)典第五戰(zhàn)-各種對(duì)話框Dialog精彩薈萃
百戰(zhàn)經(jīng)典第八戰(zhàn)-BitmapFactory.Options對(duì)資源圖片進(jìn)行縮放
百戰(zhàn)經(jīng)典第四戰(zhàn)-玩轉(zhuǎn)ListView
百戰(zhàn)經(jīng)典第十五-竊聽風(fēng)云之短信監(jiān)聽
前言
百戰(zhàn)經(jīng)典第十四戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas用戶表查詢功能實(shí)現(xiàn)
百戰(zhàn)經(jīng)典第九戰(zhàn)-ViewFlipper實(shí)現(xiàn)幻燈效果
百戰(zhàn)經(jīng)典第三戰(zhàn)-實(shí)現(xiàn)畫圖板
百戰(zhàn)經(jīng)典第十七戰(zhàn)-基于加速度傳感器的搖一搖功能實(shí)例
百戰(zhàn)經(jīng)典第十戰(zhàn)-LayoutAnimation布局動(dòng)畫效果
百戰(zhàn)經(jīng)典第七戰(zhàn)-顯示倒計(jì)時(shí)的Button按鈕
百戰(zhàn)經(jīng)典第六戰(zhàn)-Activity啟動(dòng)模式小樣
百戰(zhàn)經(jīng)典第十二戰(zhàn)-GridView動(dòng)態(tài)刪除Item
百戰(zhàn)經(jīng)典第十六戰(zhàn)-圖片或頭像設(shè)置功能
百戰(zhàn)經(jīng)典第十九戰(zhàn)-短信監(jiān)聽實(shí)現(xiàn)驗(yàn)證碼自動(dòng)填入
百戰(zhàn)經(jīng)典第一戰(zhàn)—聽話的TextView
百戰(zhàn)經(jīng)典第十八戰(zhàn)-自定義控件實(shí)現(xiàn)一鍵清空輸入框
百戰(zhàn)經(jīng)典第十三戰(zhàn)-網(wǎng)絡(luò)交互,基于Baas實(shí)現(xiàn)用戶注冊(cè)功能

百戰(zhàn)經(jīng)典第七戰(zhàn)-顯示倒計(jì)時(shí)的Button按鈕

最近在做獲取驗(yàn)證碼的功能,考慮到優(yōu)良的用戶體驗(yàn),在用戶點(diǎn)擊獲取驗(yàn)證碼后,Button變得不可點(diǎn),同時(shí)上面會(huì)顯示一個(gè)倒計(jì)時(shí),倒計(jì)時(shí)結(jié)束后Button變得可點(diǎn)擊。結(jié)合一些材料寫了一個(gè)小Demo,大家可以應(yīng)用到自己的項(xiàng)目中。

一、代碼

1.activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <Button
        android:id="@+id/btn_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="獲取驗(yàn)證碼" />
</RelativeLayout>

布局文件很簡(jiǎn)單,就一個(gè)button按鈕。

2.MainActivity.java:

package com.example.timebutton;
//省略import
public class MainActivity extends Activity {
    private Button mTimeButton;
    private TimeCount time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mTimeButton = (Button) findViewById(R.id.btn_time);
        time = new TimeCount(60000, 1000);
        mTimeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                time.start();
            }
        });
    }
    /**
     * 繼承倒計(jì)時(shí)類
     * @author 
     */
    class TimeCount extends CountDownTimer {
        /**
         * 構(gòu)造方法
         * @param millisInFuture
         *            總倒計(jì)時(shí)長(zhǎng) 毫秒
         * @param countDownInterval
         *            倒計(jì)時(shí)間隔
         */
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeButton.setEnabled(false);
            mTimeButton.setText(millisUntilFinished / 1000 + "秒");
        }
        @Override
        public void onFinish() {// 計(jì)時(shí)結(jié)束
            mTimeButton.setEnabled(true);
            mTimeButton.setText("重新獲取");
        }
    }
}

這里用到了TimeCount類,初始化時(shí)傳入兩個(gè)參數(shù)millisInFuture和countDownInterval,millisInFuture表示從開始調(diào)用start()到倒計(jì)時(shí)完成并onFinish()方法被調(diào)用的毫秒數(shù),即一個(gè)周期;countDownInterval表示接收onTick(long)回調(diào)的間隔時(shí)間。本實(shí)例中采用60秒周期,1秒間隔。TimeCount 繼承自CountDownTimer 類,覆寫了里面的onTick方法,此方法是在計(jì)數(shù)過(guò)程中執(zhí)行,借用setEnabled方法,將button變成不可點(diǎn)擊的狀態(tài),同時(shí)更新button上顯示的時(shí)間。覆寫的onFinish方法,在計(jì)時(shí)結(jié)束后觸發(fā),將button設(shè)置成可點(diǎn)擊的狀態(tài),并將button上的文字變成“重新獲取”。

二、運(yùn)行實(shí)例:

這里寫圖片描述 這里寫圖片描述 這里寫圖片描述