Android:实现定时器 Handler的postDelayed(Runnable, long)方法

先演示一个Demo代码。功能是每隔一秒实现Textview颜色的变化,而且是循环变化直到停止。

package com.example.handlerpost;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	Button mBtok;
	Button mBtCancel;
	TextView mNumber;
	private int mRunCount = 0;
	private int TIME = 1000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mBtok = (Button) findViewById(R.id.btok);
		mBtCancel = (Button) findViewById(R.id.btCancel);
		mNumber = (TextView) findViewById(R.id.number);
		mBtCancel.setOnClickListener(this);
		mBtok.setOnClickListener(this);

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btok:
			mHandler.removeCallbacks(mRunnable);
			mHandler.postDelayed(mRunnable, 0);

			break;
		case R.id.btCancel:
			closeTimer();

			break;

		}
	}

	private void closeTimer() {
		mRunCount = 0;
		mHandler.removeCallbacks(mRunnable);
		mNumber.setTextColor(android.graphics.Color.parseColor("#FF4040"));//最初的颜色

	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			default:
				break;
			}
		}
	};

	Runnable mRunnable = new Runnable() {
		@Override
		public void run() {
			switch (mRunCount) {
			case 0:
				mNumber.setTextColor(android.graphics.Color.parseColor("#FF4040"));
				break;
			case 1:
				mNumber.setTextColor(android.graphics.Color.parseColor("#FF3E96"));
				break;
			case 2:
				mNumber.setTextColor(android.graphics.Color.parseColor("#FF1493"));
				break;
			case 3:
				mNumber.setTextColor(android.graphics.Color.parseColor("#FF00FF"));
				break;
			case 4:
				mNumber.setTextColor(android.graphics.Color.parseColor("#FF0000"));
				break;
			case 5:
				mNumber.setTextColor(android.graphics.Color.parseColor("#C1C1C1"));
				break;
			case 6:
				mNumber.setTextColor(android.graphics.Color.parseColor("#C0FF3E"));
				break;
			case 7:
				mNumber.setTextColor(android.graphics.Color.parseColor("#BF3EFF"));
				break;
			case 8:
				mNumber.setTextColor(android.graphics.Color.parseColor("#BC8F8F"));
				break;
			case 9:
				mNumber.setTextColor(android.graphics.Color.parseColor("#9AFF9A"));
				break;
			case 10:
				mNumber.setTextColor(android.graphics.Color.parseColor("#0000EE"));
				mRunCount = -1;
				break;

			}
			mRunCount++;

			// handler的postDelayed方法实现定时器
			try {
				mHandler.postDelayed(this, TIME);
			} catch (Exception e) {
				e.printStackTrace();
				mRunCount = 0;
			}
		}
	};

}

1,首先创建一个Handler对象
Handler mHandler=new Handler();
2,然后创建一个Runnable对象
Runnable mRunnable=new Runnable(){
   @Override
   public void run() {
    //要做的事情,这里再次调用此Runnable对象,以实现每秒实现一次的定时器操作

     mHandler.postDelayed(this, 1000);
   } 
};
3,mHandler.postDelayed(runnable, 1000);使用PostDelayed方法,1秒后调用此Runnable对象
4. 定义int类型变量mRunCount 变量和定时器结合,每走1S变量加1,到最后一个值加1后为0,然后继续循环。
5,mHandler.removeCallbacks(runnable); 用此方法关闭循环定时器
 
  

你可能感兴趣的:(Android,Handler,Android,定时器)