Events for Long click down and long click up in Android

随声附和 提交于 2019-11-29 11:38:57
Jiyong Park

onKeyXXX() methods are for Key Events from keyboard or hard keys such as menu key, search key, and so on.

If you want to detect touch events, which is called MotionEvent in Android, you have to override onTouchEvent(MotionEvent e) method and use GestureDetector class for identifying long press.

private GestureDetector mGestureDetector;

public FfwRewButton(...) {
    //....
    mGestureDetector = new GestureDetector(context, 
        new GestureDetector.SimpleOnGestureListener() {
            public boolean onDown(MotionEvent e) {
                mLongClicked = false;
                return true;
            }
            public void onLongPress(MotionEvent e) {
                mLongClicked = true;
                // long press down detected
            }
        });
    }

    public boolean onTouchEvent(MotionEvent e) {
        mGestureDetector.onTouchEvent(e);
        if (mLongClicked && e.getAction() == ACTION_UP) {
           // long press up detected
        }
    }
}

Something like this will get you on the right path,

I didn't compile so you may have to correct a few syntax things but your goal can be achieved with this concept

OnTouchListener mTouchListener = new OnTouchListener(){
  private totalTimeDown = -1;
  private downTime = -1;
  public boolean onTouch(View v, MotionEvent me){
    if(me.getAction() == MotionEvent.ACTION_DOWN){
        downTime = System.getCurrentTimeInMillis();
        return true;
    }

    if(me.getAction() == MotionEvent.ACTION_UP){
        totalTimeDown = System.getCurrentTimeInMillis() - downTime;
        if(totalTimeDown > 500){
            //Finger was down long enough for "longClick"
            return true;
        }
    }
    return false;
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!