xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 使用自定义View时应使用全限定类名 -->
<org.crazyit.event.MyButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单击我"
/>
</LinearLayout>
CallbackHandler.java
package org.crazyit.event;
import android.app.Activity;
import android.os.Bundle;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class CallbackHandler extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
MyButton.java
/**
*
*/
package org.crazyit.event;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class MyButton extends Button
{
public MyButton(Context context , AttributeSet set)
{
super(context , set);
// TODO Auto-generated constructor stub
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
super.onKeyDown(keyCode , event);
Log.v("-crazyit.org-" , "the onKeyDown in MyButton");
//返回true,表明该事件不会向外扩散
return true;
}
}
基于回调的事件处理更适合那种事件处理逻辑比较固定的View
跟随手指的小球
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- 使用自定义组件 -->
<org.crazyit.event.DrawView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
/**
*
*/
package org.crazyit.event;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class DrawView extends View
{
public float currentX = 40;
public float currentY = 50;
/**
* @param context
*/
public DrawView(Context context , AttributeSet set)
{
super(context , set);
}
@Override
public void onDraw (Canvas canvas)
{
super.onDraw(canvas);
//创建画笔
Paint p = new Paint();
//设置画笔的颜色
p.setColor(Color.RED);
//绘制一个小圆(作为小球)
canvas.drawCircle(currentX , currentY , 15 , p);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
//当前组件的currentX、currentY两个属性
this.currentX = event.getX();
this.currentY = event.getY();
//通知改组件重绘
this.invalidate();
//返回true表明处理方法已经处理该事件
return true;
}
}
package org.crazyit.event;
import org.crazyit.event.R;
import android.app.Activity;
import android.os.Bundle;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class CustomView extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}