我根据此代码在按钮上放置了onLongPress和onDoubleTap操作:

...
GestureDetector detector = new GestureDetector(this, new TapDetector());

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    detector = new GestureDetector(this, new TapDetector());
    ...
}

private class TapDetector extends GestureDetector.SimpleOnGestureListener {

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // Do something
        return true;
    }

    @Override
     public void onLongPress(MotionEvent e) {
        // Do something
    }
}

Button incomeButton = (Button) findViewById(R.id.income);
button.setOnTouchListener(new OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        detector.onTouchEvent(event);
        return true;
    }
});

在onDoubleClick触发并执行后,我总是会看到onLongPress被触发。这种反直觉行为的原因是什么,如何避免呢?

更新了
我已将源代码更改为更具体
public class MainActivity extends Activity {
private GestureDetector detector;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    detector = new GestureDetector(this, new TapDetector());

    Button button = (Button) findViewById(R.id.button);
    button.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("************* onTouch *************");
            detector.onTouchEvent(event);
            return true;
        }
    });
}

class TapDetector extends GestureDetector.SimpleOnGestureListener {

    @Override
     public void onLongPress(MotionEvent e) {
        System.out.println("************* onLongPress *************");
    }

    @Override
     public boolean onDoubleTap(MotionEvent e) {
        System.out.println("************* onDoubleTap *************");
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), NewActivity.class);
        intent.putExtra("parameterName", "parameter");
        startActivity(intent);
        return true;
    }
}
}

这是我单击DoubleTap 上之后的日志。您可以在最后看到onLongPress-我从不单击它。
I/System.out( 1106): ************* onTouch *************
I/System.out( 1106): ************* onTouch *************
I/System.out( 1106): ************* onTouch *************
I/System.out( 1106): ************* onDoubleTap *************
I/ActivityManager(   59): Starting activity: Intent { cmp=my.tapdetector/.NewActivity (has extras) }
I/ActivityManager(   59): Displayed activity my.tapdetector/.NewActivity: 324 ms (total 324 ms)
I/System.out( 1106): ************* onLongPress *************

更新我找到了解决方案。为了避免onLongPress触发,需要完成两个更改:

首先:detector.setIsLongpressEnabled( true );在onTouch中(查看v,MotionEvent事件)
    button.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("************* onTouch *************");
            detector.onTouchEvent(event);
            detector.setIsLongpressEnabled(true);
            return true;
        }
    });

第二:添加detector.setIsLongpressEnabled( false );在onDoubleTap(MotionEvent e)中
     public boolean onDoubleTap(MotionEvent e) {
        detector.setIsLongpressEnabled(false);
        System.out.println("************* onDoubleTap *************");
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), NewActivity.class);
        intent.putExtra("parameterName", "parameter");
        startActivity(intent);
        return true;
    }

最佳答案

从技术上讲这不应该发生

case MotionEvent.ACTION_DOWN:
        mLastMotionX = x;
        mLastMotionY = y;
        mCurrentDownEvent = MotionEvent.obtain(ev);
        mAlwaysInTapRegion = true;
        mInLongPress = false;

        if (mIsLongpressEnabled) {
            mHandler.removeMessages(LONG_PRESS);
            mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime()
                    + tapTime + longpressTime);
        }
        mHandler.sendEmptyMessageAtTime(SHOW_PRESS, mCurrentDownEvent.getDownTime() + tapTime);

因为在ACTION_DOWN或ACTION_UP事件上,所有LONG_PRESS消息事件都从队列中删除。因此,在第二次点击时,以下代码将删除长按事件。
 mHandler.removeMessages(LONG_PRESS);


     @Override
     public void onLongPress(MotionEvent e) {
        if(MainActivity.this.hasWindowFocus())
        {
            Log.d("Touchy", "Long tap");
        }
    }

关于android - 为什么在onDoubleTap之后总是触发android onLongPress?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7606921/

10-11 20:09