Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android CountDownTimer倒計時簡單使用

Android CountDownTimer倒計時簡單使用

編輯:關於Android編程

CountDownTimer:

Schedule a countdown until a time in the future, with regular notifications on intervals along the way.

倒計時這個類比較簡單,可以學習這樣來設計類,下面看這個類的一個簡單應用的例子:

這麼個小例子也需要優化,優化之前什麼也沒考慮只實現了效果, 簡單優化下增加了了多次點擊Dlialog不重復創建和取消countdowntimer任務的代碼;

簡單優化後:

package com.example.dliagdemo;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button btnSure;
	private MainActivity instance;
	private CountDownTimer mDownTimer;
	private Dialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		instance = this;
		Button btn = (Button) findViewById(R.id.btn);
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (dialog == null)
					showDialog();
				else if (!dialog.isShowing())
					showDialog();
			}
		});

	}

	private void showDialog() {
		dialog = new Dialog(instance);
		LayoutInflater inflater = LayoutInflater.from(instance);
		View v = inflater.inflate(R.layout.itmes, null);
		btnSure = (Button) v.findViewById(R.id.buttonSure);
		btnSure.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(instance, "游戲開始", Toast.LENGTH_SHORT).show();
				if (dialog != null) {
					dialog.dismiss();
				}
			}
		});
		mDownTimer = new CountDownTimer(10000, 1000) {

			public void onTick(long millisUntilFinished) {
				if (btnSure != null) {
					btnSure.setText("自動開始(" + millisUntilFinished / 1000	+ "/s)");
				}
			}

			public void onFinish() {
				if (btnSure != null) {
					btnSure.performClick();
				} 
			}
		}.start();
		dialog.setContentView(v);
		dialog.setTitle("開始游戲");
		dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (mDownTimer != null) {
					mDownTimer.cancel(); 
					mDownTimer = null;
				}
			}
		});
		dialog.show();
	}
}

只簡單實現倒計時的效果。

package com.example.dliagdemo;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button btnSure;
	private MainActivity instance;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		instance =this;
		Button btn = (Button)findViewById(R.id.btn);
		btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog();  
			}
		});

	}
	private void showDialog() {
		final Dialog dialog = new Dialog(instance); 
		LayoutInflater inflater = LayoutInflater.from(instance);
		View v =inflater.inflate(R.layout.itmes, null);
		btnSure = (Button)v.findViewById(R.id.buttonSure);
		btnSure.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(instance, "游戲開始", Toast.LENGTH_SHORT).show();
				if (dialog!=null&&dialog.isShowing()) { 
					dialog.dismiss();
				}
			}
		});
		new CountDownTimer(10000, 1000) {//總時間, 間隔時間

			public void onTick(long millisUntilFinished) {
				btnSure.setText("即將開始(" + millisUntilFinished / 1000+"/s)");
			}

			public void onFinish() {
				if (dialog!=null&&dialog.isShowing()) {
					btnSure.performClick();//點擊按鈕
					dialog.dismiss();
				}
			}
		}.start(); 
		dialog.setContentView(v);
		dialog.setTitle("開始游戲");
		dialog.show();
	}  
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved