Android的:如何检测如果使用触摸并拖动了按钮区域? [英] Android: How to detect if use touches and drags out of button region?

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

问题描述

在Android上,我们如何可以检测是否按钮并拖动用户触摸了这个按钮的区域?

In Android, how can we detect if user touches on button and drag 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

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

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