In the application on Android I use 10 buttons and focus when clicking the button.

If I select button 1, it becomes in focus, if I select button 2, then there are already two buttons in focus. I need to take the buttons one by one into focus, that is, if I select button 2, then the first one loses the focus, and the second one appears.

Code for buttons:

View.OnTouchListener list = new View.OnTouchListener() { public boolean onTouch(View view, MotionEvent event { if(event.getAction()==MotionEvent.ACTION_DOWN){ view.setPressed(true); switch (view.getId()) { case R.id.bt0: break; case R.id.bt1: break; case R.id.bt2: break; ............... } } return true; } }; 
  • Perhaps you want to organize your RadioButton? He is already there, and he solves part of your problem - Egor Randomize
  • This is not exactly that, I use the usual Button, when choosing it changes its color - Vladimir
  • one
    You can customize the radio button for any type, including the usual buttons. Making your own implementation of what is already in the API is not practical. - pavlofff
  • I will come home and try to look in android studio radiobutton, I want to minimize the process with such an easy idea, I do not want to download the code in general because of such a trifle - Vladimir
  • one
    Here is a good example - pavlofff

1 answer 1

Try this

create a method in which you will focus off the required buttons

 private void removeFocuseAllButton(){ //например так (можно лист перебрать в цикле) //(или проверять, если есть фокус - снимать) button1.clearFocus(); button2.clearFocus(); button3.clearFocus(); ... //10 кнопок думаю не проблема пробежать (: } 

in the listener after the event, call this method, then as you intended

 View.OnTouchListener list = new View.OnTouchListener() { public boolean onTouch(View view, MotionEvent event { if(event.getAction()==MotionEvent.ACTION_DOWN){ removeFocuseAllButton();//здесь удаляем везде фокус view.setPressed(true); switch (view.getId()) { case R.id.bt0: break; case R.id.bt1: break; case R.id.bt2: break; ............... } } return true; } };