首页 > 代码库 > 浅议事件异步处理底层实现原理

浅议事件异步处理底层实现原理

//主类
package cn.com.likeshow.bluetoothchat;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		
		final TextView tv=new TextView(getApplicationContext());
		MyButton  but=new MyButton(getApplicationContext());
		
		LinearLayout lin=new LinearLayout(getApplicationContext());
		lin.setOrientation(LinearLayout.VERTICAL);
		
		lin.addView(tv);
		lin.addView(but);
		setContentView(lin);
		

		//字定义按钮添加监听器 
		//创建你匿名MyListener对象,传给but的setOnClickListener()方法
		but.setOnClickListener(new MyListener() {
			@Override
			public void onClick(MyEvent e) {
				tv.setText("MyEvent事件 "+e.getMessage());
			}
		});
	}



}


=====================================================================================================

//MyView接口
package cn.com.likeshow.bluetoothchat;

public interface MyView {
   //所有控件都用的方法,只能被系统调用
	public void systemEventMetod(MyEvent e);
}


 

========================================================================================

//MyListener接口
package cn.com.likeshow.bluetoothchat;

import android.view.View;


public interface MyListener {

	public void onClick(MyEvent e);
}


 

===================================================================

//MyEvent简单事件封装类
package cn.com.likeshow.bluetoothchat;

public class MyEvent {

	private String eventStr;
	public MyEvent(String eventStr)
	{
		this.eventStr=eventStr;
	}
	
	public String  getMessage()
	{
		return eventStr;
	}
}


 

=============================================

//自定义按钮 
package cn.com.likeshow.bluetoothchat;

import java.util.Date;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class MyButton extends View implements MyView {

	private MyListener lis;
	
	public MyButton(Context context) {
		super(context);
		 
	}
	

	/**
	 * 
	 * 所用控件都实现的一组规范(即:函数   此处假定该函数是public void systemEventMetod(MyEvent e)  系统会调用该函数)
	 * 即:当该按钮被点击时,系统自动调用MyView基类中的systemEventMetod(MyEvent e)方法
	 */
	@Override  
	public void systemEventMetod(MyEvent e) {
		//防止为调用 setOnClickListener(MyListener lis)时抛空指针异常
		if(lis==null)
			return;
		//通过多态完成调用自己实现的方法
		lis.onClick(e);
	}
	
	//注册
	public void setOnClickListener(MyListener lis)
	{
		this.lis=lis;
	}

	//绘制按钮界面
	@Override
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		Paint p=new Paint();
		p.setColor(Color.GRAY);
		
		canvas.drawRect(10,80,100,120, p);
		
		p.setColor(Color.RED);
		canvas.drawText("按钮", 20, 100, p);

	}
	
	
	
	
	
	//模拟点击按钮后,系统自动调用MyView基类中的  systemEventMetod(MyEvent e)方法
	//每当创建MyButton时,系统会得到该MyButton对象的引用,当系统获取到屏幕点击时,会自动调用MyButton
	//的  systemEventMetod(MyEvent e)方法
	@Override  
    public boolean onTouchEvent(MotionEvent event) {  
        String s=new Date().toLocaleString();
        
		this.systemEventMetod(new MyEvent(s));
		
        return super.onTouchEvent(event);  
    }  
}