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;
				}
			}
		}
	}
}


效果:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值