How to determine a long touch on android?

后端 未结 5 2158
走了就别回头了
走了就别回头了 2021-02-12 20:29

I am looking for a way for when a user long touches a mapview (lets say for 1000ms) that i can some how do a certain action.

How would i go about judging how long a use

5条回答
  •  梦毁少年i
    2021-02-12 20:46

    You can set up a longClickListener and a touchListener. Add a boolean class data member variable longClicked and set it to false initially. This is how you can set the longClickListener.

        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                longClicked = true;
                return false;
            }
        });
    

    For touchListener

        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(longClicked){
                    //Do whatever you want here!!
                    longClicked = false;
                }
                return false;
            }
        });
    

    It will give you the same effect as google maps long click. Hope it helps.

提交回复
热议问题