安卓:如果检测用户触摸和拖拽出一个按钮区域的? [英] Android: Detect if user touches and drags out of button region?

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

问题描述

在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,新的可能性的世界打开:
<一href=\"http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_HOVER_ENTER\">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

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

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