Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android倒計時功能的實現

Android倒計時功能的實現

編輯:關於Android編程

Android中的倒計時的功能(也可以直接使用CountDownTimer這個類直接實現,相關此Demo可查看我的博客),參考了網上寫的很好的一個倒計時Demo:

\

下面提供下代碼,和大家一起分享一下:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+TWFpbkFjdGl2aXR5o7o8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">package com.example.mytime; import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { Button startButton; EditText minuteText; EditText secondText; int minute; int second; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); startButton = (Button) findViewById(R.id.button_start); minuteText = (EditText)findViewById(R.id.minute); secondText = (EditText)findViewById(R.id.second); startButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (!minuteText.getText().toString().equals("")) { minute = Integer.parseInt(minuteText.getText().toString()); } if (!secondText.getText().toString().equals("")) { second = Integer.parseInt(secondText.getText().toString()); } if (minute != 0 || second != 0) { ArrayList list = new ArrayList(); list.add(minute); list.add(second); Intent intent = new Intent(); intent.setAction("com.example.mytime.StartActivity"); intent.putIntegerArrayListExtra("times", list); startActivity(intent); } } }); } @Override protected void onResume() { minute = 0; second = 0; super.onResume(); } }
具體倒計時相關類StartActivity:

package com.example.mytime;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.TextView;

public class StartActivity extends Activity{

	static int minute = -1;
	static int second = -1;
	final static String tag = "tag";
	TextView timeView;
	Timer timer;
	TimerTask  timerTask;
	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			System.out.println("handle!");
			if (minute == 0) {
				if (second == 0) {
					timeView.setText("Time out !");
					if (timer != null) {
						timer.cancel();
						timer = null;
					}
					if (timerTask != null) {
						timerTask = null;
					}
				}else {
					second--;
					String minutes = String.valueOf(minute);
					String minute = minutes;
					minute = "";
					if (second >= 10) {
						minutes = String.valueOf(minute);
						minute = minutes;
						minute = "";
						timeView.setText(minute + second);/*"0"+minute + ":" + */
					}else {
						timeView.setText(minute + "0" + second);/*"0"+minute + ":0" + */
					}
				}
			}else {
				if (second == 0) {
					second =59;
					minute--;
					if (minute >= 10) {
						timeView.setText(minute + ":" + second);
					}else {
						timeView.setText("0"+minute + ":" + second);
					}
				}else {
					second--;
					if (second >= 10) {
						if (minute >= 10) {
							timeView.setText(minute + ":" + second);
						}else {
							timeView.setText("0"+minute + ":" + second);
						}
					}else {
						if (minute >= 10) {
							timeView.setText(minute + ":0" + second);
						}else {
							timeView.setText("0"+minute + ":0" + second);
						}
					}
				}
			}
		};
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start);
		timeView = (TextView)findViewById(R.id.myTime);
		
		if (minute == -1 && second == -1) {
			Intent intent = getIntent();
			ArrayList times = intent.getIntegerArrayListExtra("times");
			minute = times.get(0);
			second = times.get(1);
		}
		
		timeView.setText(minute + ":" + second);
		
		timerTask = new TimerTask() {
			
			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 0;
				handler.sendMessage(msg);
			}
		};
		
		timer = new Timer();
		timer.schedule(timerTask,0,1000);
		
	}
	
	@Override
	protected void onDestroy() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (timerTask != null) {
			timerTask = null;
		}
		minute = -1;
		second = -1;
		super.onDestroy();
	}
}


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