android    o…

最近有看到一个问题,原文如下:

我用WindowManager.addView(view, params)方法添加了一个view,当我实现这个view的setOnTouchListener和setOnClickListener时,view只能监听ontouch事件,监听不到onclick事件,我去掉setOnTouchListener后就能监听到onclick事件,这是怎么回事?如何让这个view能监听到这2中事件?


发现有很多人对这个知识点有点模糊,回答之后,把自己的理解整理下,随便在这里分享一下。

下面是回答原文:


【0】这是因为回调函数的返回值所引起的。

 

onClick是不需要返回值的,而onTouch的返回值是boolean.
关于onTouch的返回值,官方解释如下:
*
This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.
*
如果在Touch事件的down action的时候返回false,则表示这个组件不会吃掉这个down event,并且对touch的后续动作都不感兴趣。那么整个touch的动作,从一开始刚点击下去的时候,你就不会再被回调到了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值