One method for implementing onTouchListener () for multiple buttons

I'm trying to figure out if there is a way to create a single method for implementing a touch listener for multiple buttons, since I have quite a few buttons that do pretty much the same thing. The only difference is what they do is the message they will send via my sendMessage () method, and how long you need to hold the button to send the message. If there is a way to do this, what could it be? And also, why not like this work?

//Within onCreate Method... Button mButton = (Button) findViewbyId(R.id.three_sec_button); mButton = addTouchTimer(mButton, 3, 3); 

Calls -

 private Button addTouchTimer(Button button, final int sec, final int messageNum){ button.setOnTouchListener(new View.OnTouchListener() { boolean longEnough = false; long realTimeLeft = sec * 1000; @Override // This will make it so that a message is only sent if the button is held down for 3 seconds // Otherwise it won't send. It is sent during the hold down process, releasing it returns a false // value and no message is sent. public boolean onTouch(View arg0, MotionEvent arg1) { Log.d("Button", "Touchy Touchy!"); if(arg1.getAction() == MotionEvent.ACTION_DOWN){ buttonPressTime = new CountDownTimer(realTimeLeft, 1000){ @Override public void onTick(long millisUntilDone){ realTimeLeft = millisUntilDone; } @Override public void onFinish() { long timeLeft = realTimeLeft; long currTime = System.currentTimeMillis(); long realFinishTime = currTime + timeLeft; while(currTime < realFinishTime){ currTime = System.currentTimeMillis(); } longEnough = true; sendEmergencyMessage(longEnough, messageNum); } }.start(); } else if(arg1.getAction() == MotionEvent.ACTION_UP){ buttonPressTime.cancel(); sendMessage(longEnough, messageNum); } return longEnough; } }); return button; } 

In terms of efficiency, there seems to be a better way to do this than to implement separate listeners for each button. As a side note, sendMessage () has a log call in it that uses a boolean, I want to see what it is installed when it is passed. This is the only reason it is called when the button is released.

+6
source share
6 answers

Yes, you are right, there is a better way. One TouchListener that processes everything, determining which button is found through the identifier.

 void intialization(){ Button m1, m2, m3, m4; ... //do initialization stuff m1.setId(1); m2.setId(2); m3.setId(3); m4.setId(4); MyTouchListener touchListener = new MyTouchListener(); m1.setOnTouchListener(touchListener); m2.setOnTouchListener(touchListener); m3.setOnTouchListener(touchListener); m4.setOnTouchListener(touchListener); } public class MyTouchListener implements OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { switch(v.getId()){ case 1: //do stuff for button 1 break; case 2: //do stuff for button 2 break; case 3: //do stuff for button 3 break; case 4: //do stuff for button 4 break; } return true; } } 

And how do you do it! In this case, a numerical approach to the identifier is very useful. Another approach is for your activity to run OnTouchListener in your activity, and then your code would be even simpler.

 public class MyActivity extends Activity implements OnTouchListener { void initialization(){ Button m1, m2, m3, m4; ... //do initialization stuff m1.setId(1); m2.setId(2); m3.setId(3); m4.setId(4); m1.setOnTouchListener(this); m2.setOnTouchListener(this); m3.setOnTouchListener(this); m4.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { switch(v.getId()){ case 1: //do stuff for button 1 break; case 2: //do stuff for button 2 break; case 3: //do stuff for button 3 break; case 4: //do stuff for button 4 break; } return true; } } 

Note. This approach will also work for OnClickListener, OnCheckedChangeListener or any other listener that you would install on the Android screen.

+17
source

With ButterKnife it will be so. (in my case, ImageView is like buttons)

 @OnTouch({R.id.Button1, R.id.Button2, R.id.Button3}) public boolean buttonsTouched(ImageView button, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: --(Your ACTION on Pressed)-- return true; case MotionEvent.ACTION_UP: --(Your ACTION on Release)-- return true; } return true; } 
+5
source

Yes, there is a better way to do the same.
1) Add your class to OnTouchListener .
2) Add this listener to each button that should handle the touch event. Like this:

 button1.setOnTouchListener(this); 

3) And in this public boolean onTouch(View arg0, MotionEvent arg1) {});

you can use the switch case on the object in question. The first argument, i.e. arg0 is the view to which the touch event was sent. In your case, these will be different buttons. Something like that:

 public boolean onTouch(View arg0, MotionEvent arg1) { if (arg1.getAction() == MotionEvent.ACTION_DOWN) { switch (arg0.getId()) { case R.id.button1: // Id of the button // Your code goes here break; case R.id.button2: // Id of the button // Your code goes here break; default: break; } } return true; } 
+4
source
 OnTouchListener mOnTouchListener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // Code goes here return true; } }; button1.setOnTouchListener(mOnTouchListener); button2.setOnTouchListener(mOnTouchListener); 
+1
source

I combine two answers, this is my code

 public class MyActivity extends Activity implements OnTouchListener { Button mGreen, mRed; void initialization() { ... //do initialization stuff mGreen.setOnTouchListener(this); mRed.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: actionDown(); break; case MotionEvent.ACTION_UP: actionUp(); break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_POINTER_UP: break; case MotionEvent.ACTION_MOVE: actionMove(); break; } return true; } public void actionDown() { switch (view.getId()) { case R.id.button_green: //todo break; case R.id.button_red: //todo break; } } public void actionUp() { switch (view.getId()) { case R.id.button_green: //todo break; case R.id.button_red: //todo break; } } public void actionMove() { switch (view.getId()) { case R.id.button_green: // todo break; case R.id.button_red: // todo break; } }} 

I hope this code helps someone

+1
source

Why don't you use an oil knife?

 @Nullable @OnClick({R.id.btn1, R.id.btn2,R.id.btn3, R.id.btn4}) public void doStuff(Button button) {} 
0
source

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


All Articles