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();
}
}
});