Android:单击和长按/双击有两种不同的活动?

Gid*_*zer 5 events android tap listener

我正在尝试开发一个功能,其中单击一个项目将调用Intent转到另一个Activity,并且长按或双击该项目会执行其他操作,例如允许您编辑文本.

到目前为止,我只能在同一时间发生这两种情况,但不能单独发生.有没有人有任何想法?

public boolean onTouchEvent(MotionEvent e) {
    return gestureScanner.onTouchEvent(e);
}


public boolean onSingleTapConfirmed(MotionEvent e) { 
    Intent i = new Intent(getContext(), SecondClass.class);
    getContext().startActivity(i);

    return true; 
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; }
public void onLongPress(MotionEvent e) {
    Toast.makeText(getContext(), "Edit feature here", Toast.LENGTH_SHORT).show();

}
Run Code Online (Sandbox Code Playgroud)

Gid*_*zer 6

我设法解决了这个问题.原来,所有我需要做的是改变返回值falsetrueonDown()处理程序.

public boolean onTouchEvent(MotionEvent e) {
    return gestureScanner.onTouchEvent(e);
}

public boolean onSingleTapConfirmed(MotionEvent e) { 

    Intent i = new Intent(getContext(), SecondClass.class);
    getContext().startActivity(i);

    return true; 
}

public boolean onDown(MotionEvent e) { return true; }


public void onLongPress(MotionEvent e) {
    Toast.makeText(getContext(), "Edit Feature", Toast.LENGTH_SHORT).show();

}
Run Code Online (Sandbox Code Playgroud)


Sam*_*Sam 5

使用 GestureDetector,SimpleOnGestureListener具有onSingleTapConfirmed()您想要的、onLongPress()和方法onDoubleTap()

  • 我一直在使用手势检测器。如果我将 Intent 放入 onSingleTapConfirmed() 方法中,它不会执行任何操作。它只是让 onLongPress() 只需点击一下即可工作: (2认同)