1,View是所有控件的父类
2,View可以关联很多个listenner。就像一个女孩有很多个男人对她好一样。有为她卖苦力的穷叼丝,也有她拿得出手的高副帅。当女孩发生需要搬宿舍的事件的时候,这个时候穷叼丝监听到了该事件的发生,于是帮搬宿舍,当她需要去看电影和开房的时候,高副帅男友监听到了该事件,于是就跟她完成了一个很美好的夜晚。
View 的事件监听,举个例子
package com.example.listviewitem.widgets; import android.content.Context; import android.graphics.Canvas; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * 在自定义的View中定义三个监听器 */ public class MyView extends View { private OnDownActionListener mDown = null; private OnMoveActionListener mMove = null; private OnUpActionListener mUp = null; public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int x, y; if (event.getAction() == MotionEvent.ACTION_DOWN) { x = (int) event.getX(); y = (int) event.getY(); if (mDown != null) { mDown.OnDown(x, y); } return true; // 只有返回true这个控件的move和up才会响应 } else if (event.getAction() == MotionEvent.ACTION_MOVE) { x = (int) event.getX(); y = (int) event.getY(); if (mMove != null) { mMove.OnMove(x, y); } } else if (event.getAction() == MotionEvent.ACTION_UP) { x = (int) event.getX(); y = (int) event.getY(); if (mUp != null) { mUp.OnUp(x, y); } } return super.onTouchEvent(event); } // 为每个接口设置监听器 public void setOnDownActionListener(OnDownActionListener down) { mDown = down; } public void setOnMoveActionListener(OnMoveActionListener move) { mMove = move; } public void setOnUpActionListener(OnUpActionListener up) { mUp = up; } // 定义三个接口 public interface OnDownActionListener { public void OnDown(int x, int y); } public interface OnMoveActionListener { public void OnMove(int x, int y); } public interface OnUpActionListener { public void OnUp(int x, int y); } }
然后在layout的xml文件中加入该自定义控件
<!--?xml version="1.0" encoding="utf-8"?--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.example.listviewitem.widgets.myview android:id="@+id/my_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/area_point_bg"> </com.example.listviewitem.widgets.myview></linearlayout>
然后用一个activity来用它
package com.example.listviewitem; import com.example.listviewitem.widgets.MyView; import com.example.listviewitem.widgets.MyView.OnDownActionListener; import com.example.listviewitem.widgets.MyView.OnMoveActionListener; import com.example.listviewitem.widgets.MyView.OnUpActionListener; import android.app.Activity; import android.os.Bundle; public class TestListener extends Activity { private MyView view; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.listener); view = (MyView) findViewById(R.id.my_view); view.setOnDownActionListener(new OnDownActionListener() { @Override public void OnDown(int x, int y) { // TODO Auto-generated method stub System.out.println("down x = " + x + " y = " + y); } }); view.setOnMoveActionListener(new OnMoveActionListener() { @Override public void OnMove(int x, int y) { // TODO Auto-generated method stub System.out.println("move x = " + x + " y = " + y); } }); view.setOnUpActionListener(new OnUpActionListener() { @Override public void OnUp(int x, int y) { // TODO Auto-generated method stub System.out.println("up x = " + x + " y = " + y); } }); } }
这样就完成了一个自定义控件的时间监听。