日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Android CountDownTimer 类实现倒计时

發布時間:2023/12/13 综合教程 30 生活家
生活随笔 收集整理的這篇文章主要介紹了 Android CountDownTimer 类实现倒计时 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文用 Android 中的CountDownTimer 類實現倒計時功能,類似輸入手機號獲得驗證碼。界面如下所示:

1. 點擊 “開始計時” 按鈕后開始進行倒計時,

2. 倒計時過程:

3. 時間結束后按鈕變為 “重新驗證”,點擊后重新開始計時。

功能很簡單,Java 代碼如下:

public class MainActivity extends Activity {

    private TimeCount time;
    private Button checking;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        time = new TimeCount(60000, 1000);// 創建CountDownTimer對象(設置時間為60秒)
        checking = (Button) findViewById(R.id.button1);
        checking.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                time.start();// 開始計時
            }

        });
    }
    
    /**
     * 倒計時內部類
     */
    class TimeCount extends CountDownTimer {
        
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 參數:總時長,計時間隔
        }
        
        @Override
        public void onFinish() {// 計時完畢時觸發
            checking.setText("重新驗證");
            checking.setClickable(true);
        }
    
        @Override
        public void onTick(long millisUntilFinished){// 計時過程顯示
            checking.setClickable(false);
            checking.setText(millisUntilFinished /1000 + "秒");
        }
    }
}

布局代碼:

<LinearLayout 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"
    tools:context="com.example.testandroid.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_number" />
    
    <EditText 
        android:hint="@string/input_number"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        />
    
    <Button
        android:id="@+id/button1" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/begin"
        />

</LinearLayout>

主要來自:http://www.cnblogs.com/-cyb/archive/2011/12/18/Android_CountDownTimer.html,本人稍加整理。

總結

以上是生活随笔為你收集整理的Android CountDownTimer 类实现倒计时的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。