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

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

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

最近在做獲取驗證碼的功能,考慮到優(yōu)良的用戶體驗,在用戶點擊獲取驗證碼后,Button變得不可點,同時上面會顯示一個倒計時,倒計時結(jié)束后Button變得可點擊。結(jié)合一些材料寫了一個小Demo,大家可以應(yī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="獲取驗證碼" />
</RelativeLayout>

布局文件很簡單,就一個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();
            }
        });
    }
    /**
     * 繼承倒計時類
     * @author 
     */
    class TimeCount extends CountDownTimer {
        /**
         * 構(gòu)造方法
         * @param millisInFuture
         *            總倒計時長 毫秒
         * @param countDownInterval
         *            倒計時間隔
         */
        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() {// 計時結(jié)束
            mTimeButton.setEnabled(true);
            mTimeButton.setText("重新獲取");
        }
    }
}

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

二、運行實例:

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