onTouchListener接口及其onTouch方法
onTouchListener是View内部定义的一个接口,下面是onTouchListener
源码:
/**
* Interface definition for a callback to be invoked when a touch event is
* dispatched to this view. The callback will be invoked before the touch
* event is given to the view.
*/
public interface OnTouchListener {
/**
* Called when a touch event is dispatched to a view. This allows listeners to
* get a chance to respond before the target view.
*
* @param v The view the touch event has been dispatched to.
* @param event The MotionEvent object containing full information about
* the event.
* @return True if the listener has consumed the event, false otherwise.
*/
boolean onTouch(View v, MotionEvent event);
}
可以看到onTouchListener只有一个方法onTouch,onTouch就是我们在外界处理点击事件所要实现的方法。
一般我们在外界处理点击事件时,需要对一个View,例如Button或者TextView,先调用其setOnTouchListener(OnTouchListener l)方法,再传入一个内部匿名监听器new View.onTouchListener(),并实现其onTouch方法,通常我们返回true,true和false是优先级判断的依据之一,下文会解释原因,下面是setOnTouchListener
源码:
/**
* Register a callback to be invoked when a touch event is sent to this view.
* @param l the touch listener to attach to this view
*/
public void setOnTouch