Click, click and release the event on the button

How can I detect the pressed and pressed and released button states. I want to perform different functions in these states. On click, I want to call function1, I click, I want to call function2, and when I receive it, I want to call function3.

We can detect click state using View.OnClickListener . We can determine the state of a pressed and released button using View.OnTouchListener and processing ACTION_DOWN and ACTION_UP . However, I can detect these conditions individually, but not together.

Below is the code for OnCLickListener.

 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println(" clicked "); } }); 

Below is the code for OnTouchListener.

 button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: System.out.println(" pressed "); return true; case MotionEvent.ACTION_UP: System.out.println(" released "); return true; } return false; } }); 

When I set the click and touch the listeners on the button, the Click event is never raised. Instead, I get pressed and freed state.

How can I handle these three states together?

EDIT:

I added the OnClickListener and OnTouchListener code that I used.

+5
source share
4 answers

Change return true; inside case MotionEvent.ACTION_DOWN: and case MotionEvent.ACTION_UP: on return false; or break;

+10
source

Easy since Button is View :

  button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { // Pressed } else if (event.getAction() == MotionEvent.ACTION_UP) { // Released } return true; } }); 
+6
source

See the link. You can handle the click manually in the MotionEvent.ACTION_UP event on

 button.performClick(); 
0
source

clicked event enables the clicked and released state, if you want to fire the clicked event, put the method after ACTION_UP

-1
source

Source: https://habr.com/ru/post/1209302/


All Articles