Count Multiple Buttons with setOnClickListener

0

I have a question about how to correctly count the number of clicks that are made on multiple buttons of my .MainActivity

What I want to do is to count the number that they have clicked on the buttons that are shown and that each time they click on a button, increase +1. At the end of the count I want you to show something on the screen if it's more than 10 clicks.

Here I leave the code that I have made and it gives me failure:

    public class MainActivity extends AppCompatActivity {

        public Button btn01, btn02, btn03, btn04, btn05, btn06, btn07, btn08;

        public int conteo = 0;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.soni);

    // Boton 01
            btn01 = (Button) this.findViewById(R.id.btnAudi_01);
            btn01.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        conteo++;
                    }
            });

    // Boton 02    
            btn02 = (Button) this.findViewById(R.id.btnAudi_01);
            btn02.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        conteo++;
                    }
            });

    // Boton 03
    ...

            if(conteo >=10){
                Log.d("Bien", "Han pinchado mas de 10 veces");
            }else {
                Log.d("Error", "Todavia es menor que 10");}
            }
}

In the end, the Log shows nothing of the messages. It seems as if I did not consider counting

UPDATED:

Each button loads a different audio as you can see in mp_01.start (); , in button 2 load mp_02.start (); and so on until 8 buttons.

// Boton 01
btn01 = (Button) this.findViewById(R.id.btnAudi_01);
final MediaPlayer mp_01 = MediaPlayer.create(this, R.raw.sonido01);
        btn01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    conteo =  conteo+1;
                    if(conteo>10){
                        Log.d("Bien", "Le hemos dado " + conteo);                       
                        conteo = 0;
                    }else{
                       Log.d("Error", "Muestro AUDIO");
                       mp_01.start();
                    }
                }
        });
    
asked by Fumatamax 16.07.2018 в 15:13
source

3 answers

1

The condition that shows the number of clicks is not inside an event, if you send it like this it will never show you since it would only run when the app was opened. when creating the components ...

I recommend you create a method

private boolean verificarConteo(){
      if(conteo >=10){
            Log.d("Bien", "Han pinchado mas de 10 veces");
        }else {
            Log.d("Error", "Todavia es menor que 10");}
        }
}

and that method call it to the onClickListener event of the buttons after

 conteo++;
    
answered by 16.07.2018 / 15:34
source
0

For this the advisable thing is to create only a OnClickListener in which when giving click the value of variable conteo is increased and this listener assign it to all the buttons:

private int conteo = 0;

View.OnClickListener buttonListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {          
      if(conteo > 10){            
        muestra();
       //Reinicia contador.
         conteo++;
      }else{
        //Aumenta contador.
         conteo++;
      }
    }
};

Created the listener simply assign it to the buttons:

    btn01.setOnClickListener(buttonListener);
    btn02.setOnClickListener(buttonListener);
    btn03.setOnClickListener(buttonListener);
    
answered by 16.07.2018 в 17:21
0

Try using the setTag () method and getTag () which is independent for each view, with which you can assign a extra value For example:

Implement View's OnClick interface.

public class MainActivity extends AppCompatActivity implements View.OnClickListener

Add the listeners to each button:

btn01.setOnClickListener(this);
btn02.setOnClickListener(this);

. . etc ...

and you overwrite the interface method:

@Override
public void onClick(View v) {
    if(v.getTag() == null)
        v.setTag(0); // Valor por defecto

    v.setTag(((int)v.getTag() + 1)); // Aumentamos 1 al contador

    if((int)v.getTag() > 10){
        // Reinicias el Tag a su valor por defecto, si lo prefieres
        // v.setTag(0); // Valor por defecto
        Log.d("Bien", "Han pinchado mas de 10 veces");
    }
    else{
        // Reproduce el audio segun el id del boton
        startAudioAlert(v.getId);
        Log.d("Bien", "Todavia es menor que 10");
    }

}

private void startAudioAlert(int id){
    switch(id){
       case R.id.btnAudi_01:
          mp_01.start();
          break;
       case R.id.btnAudi_02:
          mp_02.start();
          break;
    }
}

And so with each id of the buttons and their corresponding audio.

    
answered by 16.07.2018 в 18:36