Java OnTouch和ONLONG在同一视图上单击
我有一个用onclick侦听器移动视图的程序:Java OnTouch和ONLONG在同一视图上单击,java,android,onclick,onlongclicklistener,Java,Android,Onclick,Onlongclicklistener,我有一个用onclick侦听器移动视图的程序: iv.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v,MotionEvent event) { iAction = event.getActionMasked(); switch (iAction) {
iv.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v,MotionEvent event) {
iAction = event.getActionMasked();
switch (iAction) {
case MotionEvent.ACTION_MOVE:
v.getLocationOnScreen(aLocation); // get absolute physical location of this View
iOffsetX = (aLocation [ 0 ] - (int) v.getX()); // subtract out this View's relative location within its parent View...
iOffsetY = (aLocation [ 1 ] - (int) v.getY()); // ...yielding the offsets that convert getRawX/Y's coords to setX/Y's coords
iNewX = (int) event.getRawX(); // get absolute physical coords of this touch
iNewY = (int) event.getRawY();
iNewX -= iOffsetX; // remove parent View's screen offset (calc'd above)
iNewY -= iOffsetY;
iNewX -= iRelX; // remove stored touch offset
iNewY -= iRelY;
v.setX(iNewX); // finally, move View to new coords (relative to its parent View)
v.setY(iNewY);
bExitValue = true;
break;
case MotionEvent.ACTION_DOWN:
if (delete){ v.setVisibility(View.GONE); }
iRelX = (int) event.getX(); // preserve offset of this touch within the View
iRelY = (int) event.getY();
bExitValue = false;
break;
case MotionEvent.ACTION_UP:
bExitValue = true;
break;
}
return (bExitValue);
}
});
这很好,在代码中,如果视图被移动,我会尝试返回true,这样OnLongClick就不会被调用。问题是,我只想在它被移动时返回true,因此在ACTION_DOWN中它返回false,因为第一次返回false,所以当视图也被移动时,会调用OnLongClick
我尝试在动作移动中使用v.setLongClickable(false),但它的效果相同
有没有办法在OnLongClick进行时中断它?为什么不让OnLongClick方法决定触发时要做什么?如果视图被移动,它可以忽略它;如果视图没有被移动,它可以执行长单击操作,可能是基于onTouch方法中设置的标志?似乎让一个触摸事件告诉视图如何对另一个触摸事件做出反应,可能是同时发生的触摸事件,可能无法正常工作。只是一个想法。我可以这么做,但电话无论如何都会震动。我明白了。我想你可以禁用默认的触觉反馈,如果需要的话,只让onLongClick调用可控震源。也许有更好的办法,但我不知道。祝你好运