设为首页 加入收藏

TOP

Activity中Handler的使用
2015-07-24 07:08:45 来源: 作者: 【 】 浏览:71
Tags:Activity Handler 使用

用Handler更新一个TextView的背景颜色在5种颜色中循环变换

Handle特点:

1,传递Message。用于接收子线程发送的数据,并用此数据配合主线程更新UI。

2,传递Runable对象。用于通过Handler绑定的消息队列,安排不同操作的执行顺序。

第一种实现方法:

import java.util.Random;

import com.example.handle.R.color;

import android.R.integer;
import android.app.Activity;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity {
	TextView tv;
	int color[];
	Handler myHandler;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv=(TextView)findViewById(R.id.tv);
		color=new int[]{
				R.color.black,
				R.color.blue,
				R.color.red,
				R.color.yellow,
				R.color.green
		};//背景颜色数组
		myHandler=new Handler();
		myHandler.post(rb);
	}
	Runnable rb=new Runnable(){
		public void run(){
			Random rd=new Random();//产生随机数
			int i=rd.nextInt(5);
			tv.setBackgroundColor(getResources().getColor(color[i]));
			myHandler.postDelayed(rb,1000);//设置延迟时间
		}
	};
}

第二种实现方法:

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class SecondActivity extends Activity {

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		new updateColor().start();
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg){
			Resources res = getResources();
			Drawable drawable = res.getDrawable(R.color.black);
			Drawable drawable1 = res.getDrawable(R.color.aqua);
			Drawable drawable2 = res.getDrawable(R.color.green);
			Drawable drawable3 = res.getDrawable(R.color.red);
			Drawable drawable4 = res.getDrawable(R.color.pink);
			Bundle b=msg.getData();
			int key=b.getInt("key");
			switch(key){
			case 0: SecondActivity.this.getWindow().setBackgroundDrawable(drawable) ;
			break;
			case 1:SecondActivity.this.getWindow().setBackgroundDrawable(drawable1);
			break;
			case 2:SecondActivity.this.getWindow().setBackgroundDrawable(drawable2);
			break;
			case 3:SecondActivity.this.getWindow().setBackgroundDrawable(drawable3);
			break;
			case 4:SecondActivity.this.getWindow().setBackgroundDrawable(drawable4);
			break;
			}
		}
	};
	class updateColor extends Thread{
		int i=0;
		public void run() {
			while(true){
				Message msg=handler.obtainMessage();
				Bundle bundle=new Bundle();
				if(i<5){
					try {
						bundle.putInt("key", i);
						msg.setData(bundle);
						i=i+1;
						handler.sendMessage(msg);
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else{
					i=i%5;
				}
			}
		}
	}
}

效果:

\\

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇VS2010编写动态链接库DLL及单元测.. 下一篇ZOJ 3403 Strange Calendar III

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: