android view不让touch,Android-View的OnTouch和OnClick事件不能并存问题

【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的动作,从一开始刚点击下去的时候,你就不会再被回调到了。

官方还有一段注意的话:

Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.

如果某个Listener在回调到的时候返回了true,那么这个event就会被吃掉,而不能继续传播给其他的Listener。

【1】所以说如果在onTouch里面是返回true,那么onClick当然就无法再被回调了。

【2】可以用下面的代码测试下:

Button button = (Button)this.findViewById(R.id.button1);

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v)

{

// TODO Auto-generated method stub

Log.d("Test","This is OnClick");

}

});

button.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event)

{

// TODO Auto-generated method stub

Log.d("Test","This is onTouch,return false");

return false;

}

});

我已经验证过,只有在onTouch返回false的情况下,onClick才能被执行,这个时候他们两个算是共存的。

当然我们还可以在onTouch里面去细分,可以根据MotionEvent的type去动态调整何时进行return true或者false。

补充知识:

【0】TouchMode : 对于触屏设备,当用户点击到屏幕的时候,设备会进入Touch Mode。如果某个组件的isFocusableInTouchMode()为true,那么当点击到这个组件的时候会触发组件进行focus。例如,EditText View,我们点击输入框的时候,会引发EditText进行focus,这个时候,可以看到光标在输入框进行跳动。那么其他组件如果没有设置isFocusableInTouchMode()为ture的话,就是touchable的,当点击的时候不会触发focus,只会触发onClick的Listener。

【1】onClick与onTouch的官方介绍:

onClick()

From View.OnClickListener. This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball.

可以看到这个onClick要么是在touchmode的情况下会被回调,要么是在非touchmode的情况下,当focus到的时候被回调。

onTouch()

From View.OnTouchListener. This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).

那么onTouch就是类似press,release或者其他类似在屏幕上的移动。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值