How to determine a long touch on android?

后端 未结 5 2221
走了就别回头了
走了就别回头了 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条回答
  •  天命终不由人
    2021-02-12 21:06

    This is how do you normally create an onLongClickListener. Try this:

    mapView.setOnLongClickListener(new OnLongClickListener() {
    
            @Override
            public boolean onLongClick(View arg0) {
    
                Toast.makeText(mapView.getContext(), "Hello 123", 2000);
    
                return false;
    
            }
        });
    

    Reference to your edit:

    This might be the way to get what you want.

    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
    public void run() {
         checkGlobalVariable();
    }
    };
    
    // Other init stuff etc...
    
    @Override
    public void onTouchEvent(MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {
        // Execute your Runnable after 1000 milliseconds = 1 second.
        handler.postDelayed(runnable, 1000);
        mBooleanIsPressed = true;
    }
    
    if(event.getAction() == MotionEvent.ACTION_UP) {
        if(mBooleanIsPressed) {
            mBooleanIsPressed = false;
            handler.removeCallbacks(runnable);
        }
    }
    }
    

    And now you can check with checkGlobalVariable function:

    if(mBooleanIsPressed == true)
    

    This is how you can handle this case. Good luck.

提交回复
热议问题