Android View的onTouchEvent方法与onTouchListener接口的onTouch方法优先级

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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值