gpt4 book ai didi

Android:检测用户是否触摸并拖出按钮区域?

转载 作者:IT王子 更新时间:2023-10-28 23:44:22 26 4
gpt4 key购买 nike

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

最佳答案

检查 MotionEvent.MOVE_OUTSIDE: 检查 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

关于Android:检测用户是否触摸并拖出按钮区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6410200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com