简体   繁体   English

Android:onTouchListener-当用户将触摸移动到视图外部时取消触摸事件

[英]Android: onTouchListener - cancel touch event when user moves touch outside of view

We've all done it, when we hold down on a button we're not sure we want to click or not, and while holding down on the screen still, we move our finger so the touch event doesn't fire. 我们都已经做到了,当我们按住一个按钮时,我们不确定是否要单击,而在按住屏幕的同时,我们移动了手指,因此不会触发触摸事件。

I'd like to mimic this behavior with my onTouchListener. 我想用我的onTouchListener模仿这种行为。 I have a list of cards. 我有一张卡片清单。 Every card has an onTouchListener that fires an Intent on touch event UP. 每张卡都有一个onTouchListener,它将在触摸事件UP上触发一个Intent。 If the user moves the finger off of the card while still holding down on the screen, I'd like to make sure that intent is not fired. 如果用户在按住屏幕的同时将手指从卡上移开,我想确保不会触发意图。

Any ideas? 有任何想法吗?

use this code 使用此代码

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_UP:
            if (event.getX() < width && event.getY() < height) {
                //finger is on the card and fire intent
            }
            break;
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM