Change long click delay

后端 未结 7 1610
不知归路
不知归路 2020-12-09 08:45

I am listening for a View\'s long click events via setOnLongClickListener(). Can I change the long click delay / duration?

7条回答
  •  粉色の甜心
    2020-12-09 09:33

    This was the simplest working solution I found to this restriction:

    //Define these variables at the beginning of your Activity or Fragment:
    private long then;
    private int longClickDuration = 5000; //for long click to trigger after 5 seconds
    
    ...
    
    //This can be a Button, TextView, LinearLayout, etc. if desired
    ImageView imageView = (ImageView) findViewById(R.id.desired_longclick_view);
    imageView.setOnTouchListener(new OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
              then = (long) System.currentTimeMillis();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
              if ((System.currentTimeMillis() - then) > longClickDuration) {
                /* Implement long click behavior here */
                System.out.println("Long Click has happened!");
                return false;
              } else {
                /* Implement short click behavior here or do nothing */
                System.out.println("Short Click has happened...");
                return false;
              }
            }
            return true;
          }
        });
    

提交回复
热议问题