There is a set of buttons, all clicks are processed using OnClick , and there are no problems with them. Two buttons are processed using OnTouch , and when you click on them the background of the button does not change.

  1. How can I make the background change if onTouch cannot remove onTouch ?
  2. Is it possible to do action processing in OnClick

Here is the code

  button_left.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int RightEngine = 0 , LeftEngine = 0; currentLog++; if(currentLog>100) currentLog= 100; RightEngine = (int) (21.7*Math.log((double)currentLog)); LeftEngine = (int) -(21.7*Math.log((double)currentLog)); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // нажатие flagButton = true; label = false; leftEngineVal = LeftEngine; rightEngineVal = RightEngine; break; case MotionEvent.ACTION_MOVE: label = false; flagButton = true; leftEngineVal = LeftEngine; rightEngineVal = RightEngine; break; case MotionEvent.ACTION_UP:// отпускание flagButton = false; label = true; currentLog=0; leftEngineVal=rightEngineVal=0; break; } return true; } }); 

Same is buttonRight

selector

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/left_pressed" /> <item android:state_pressed="false" android:drawable="@drawable/left" /> </selector> 

PS I am new to Android, do not judge strictly. Replacing pictures is done using selector, from actions I need MotionEvent.ACTION_DOWN , MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP

  • Replace onTouch with onClick - Werder
  • @Werder I Can't, I Need OnTouch Tools - Amir Shabanov
  • @Werder how to read MotionEvent in OnClick - Amir Shabanov
  • Yes, in onClick you can’t do it, so show the code and explain better what you mean by "what would the background change if you don’t remove onTouch" - Werder
  • code in the studio! Selector code and onTouch () code - Vladyslav Matviienko

1 answer 1

You can try to return false from onTouch instead of true

  • Why does this affect the background? - Amir Shabanov
  • @AmirShabanov, onTouch returns true if it handled the touch. And, apparently, if he processed it, then no further actions (including state processing for the background View ) will be performed. In general, it would probably be more correct to write return super.onTouch() - Vladyslav Matviienko
  • Thanks for the answer! I'm very grateful to you! - Amir Shabanov