Android - Detect End of Long Press

ⅰ亾dé卋堺 提交于 2019-12-17 09:40:40

问题


I am working on application in which a user needs to press and hold a button for a long time.

How can I detect the moment that the user: finishes the press or moves their touch position?

Thanks


回答1:


I think your best bet is to use a combination of the onLongClickListener() and onTouchListener() for that button. You'll need to catch certain events on the touch listener since it will trigger for every touch event.

Try something like the following:

class Blah extends Activity {
     private Button mSpeak;
     private boolean isSpeakButtonLongPressed = false;

     @Override
     public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.blahlayout);
          Button mSpeak = (Button)findViewById(R.id.speakbutton);
          mSpeak.setOnLongClickListener(speakHoldListener);
          mSpeak.setOnTouchListener(speakTouchListener);
     }

     private View.OnLongClickListener speakHoldListener = new View.OnLongClickListener() {

          @Override
          public boolean onLongClick(View pView) {
               // Do something when your hold starts here.
               isSpeakButtonLongPressed = true;
               return true;
          }
     }

     private View.OnTouchListener speakTouchListener = new View.OnTouchListener() {

          @Override
          public boolean onTouch(View pView, MotionEvent pEvent) {
               pView.onTouchEvent(pEvent);
               // We're only interested in when the button is released.
               if (pEvent.getAction() == MotionEvent.ACTION_UP) {
                    // We're only interested in anything if our speak button is currently pressed.
                    if (isSpeakButtonLongPressed) {
                         // Do something when the button is released.
                         isSpeakButtonLongPressed = false;
                    }
               }
               return false;
          }
     }
}



回答2:


These answers are pretty complicated. onClick from an OnClickListener still gets called at the end of a long press if you return false from the OnLongClickListener. That's the easiest place to detect the end of a long press.

This is especially important to know because if you implement the onTouch route while returning false from onLongClick (the default AS gives you and often what you want), your onClick code may be called at the end of your long presses without you realizing it.

Here's an example based on capturing a photo or video:

private boolean takingVideo = false;

captureButton.setOnClickListener(v -> {
    // onClick gets called after normal click or long click
    if(takingVideo) {
        saveVideo();
    } else {
        takePhoto();
    }
});

captureButton.setOnLongClickListener(v -> {
    takeVideo();

    return false;
});

private void takePhoto() {
    // Save the photo
}

private void takeVideo() {
    takingVideo = true;
    // Start capturing video
}

private void saveVideo() {
    takingVideo = false;
    // Save the video
}

As you can see, the logic becomes very straight forward when you let Android propagate the end touch event to an OnClickListener.




回答3:


I think you can use OnTouchListener for this.




回答4:


I think the onFocusChanged-Listener can be used for this.



来源:https://stackoverflow.com/questions/6183874/android-detect-end-of-long-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!