Android:检测用户是否触摸并拖出按钮区域? [英] Android: Detect if user touches and drags out of button region?

查看:23
本文介绍了Android:检测用户是否触摸并拖出按钮区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 中,我们如何检测用户是否触摸按钮并将其拖出该按钮的区域?

In Android, how can we detect if a user touches on button and drags out of region of this button?

推荐答案

检查 MotionEvent.MOVE_OUTSIDE: 检查 MotionEvent.MOVE:

Check the MotionEvent.MOVE_OUTSIDE: Check the MotionEvent.MOVE:

private Rect rect;    // Variable rect to hold the bounds of the view

public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){
        // Construct a rect of the view's bounds
        rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());

    }
    if(event.getAction() == MotionEvent.ACTION_MOVE){
        if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
            // User moved outside bounds
        }
    }
    return false;
}

注意:如果您想以 Android 4.0 为目标,一个充满新可能性的世界将打开:http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER

NOTE: If you want to target Android 4.0, a whole world of new possibilities opens: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER

这篇关于Android:检测用户是否触摸并拖出按钮区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆