当用户离开TouchEvent
区域时,我想删除按钮的Button
我通过以下方式检测到用户移动:
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
case MotionEvent.ACTION_MOVE:
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
logIt("You are OutSide...");
}
break;
任何人都可以建议如何做到这一点...... 任何答案都赞赏......谢谢
答案 0 :(得分:0)
重写这样的代码: -
案例MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
案例MotionEvent.ACTION_UP:
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
logIt("You are OutSide...");
}
break;
答案 1 :(得分:0)
试试这个......
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),v.getBottom());
break;
case MotionEvent.ACTION_MOVE:
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop()+ (int) event.getY())) {
Log.i(TAG, "You are outside");
return false;
}
break;
这个完整的代码会给你一些想法...
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),v.getBottom());
break;
case MotionEvent.ACTION_MOVE:
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop()+ (int) event.getY())) {
Log.i(TAG, "You are outside");
return false;
}
break;
}
return true;
}
答案 2 :(得分:0)
最简单的方式:
onTouchEvent(MotionEvent event){
...
case MotionEvent.ACTION_MOVE:
event.setAction(MotionEvent.ACTION_UP);
return super.onTouchEvent(event);
...
}
在这种情况下,您将拒绝与按钮相关的所有下一个事件。