How can I make a variable wait for the user to press 1 of 4 buttons?

4

Well, I'll try to be clearer.

I have a variable called int tentativa and I want it to get a specific value depending on which button the user presses.

The buttons are btPedra[0], btPedra[1], btPedra[2], btPedra[3] .

If the user presses btPedra[0] , the variable receives 0.

If the user presses btPedra[1] , the variable receives 1. And so on ...

Note: The program should wait for the user to press one of the buttons to continue.

My code looks like this:

for (contador = 0; contador < jogada; ++contador){

    tentativa = 
    // AQUI A VARIAVEL tentativa DEVE RECEBER 0, 1, 2 OU 3 DEPENDENDO DO BOTÃO PRESSIONADO.

    if (tentativa[contador] == sequencia[contador]){
        acertos++;
    } else {
        gameOver();
    }
}

In addition to the button that causes the variable to receive a value, its color will change.

I'm programming in java for android!

Complete method code!

public void inicioJogo() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; jogada <= 50; jogada++) {
                    for (contador = 0; contador < jogada; contador++) {
                        try {
                            Thread.sleep(250);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                btPedra[sequencia[contador]].setBackgroundResource(imagensHover[sequencia[contador]]);
                            }
                        });
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                btPedra[sequencia[contador]].setBackgroundResource(imagensNormal[sequencia[contador]]);
                            }
                        });
                        try {
                            Thread.sleep(250);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    for (contador = 0; contador < btPedra.length; ++contador){
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                btPedra[contador].setEnabled(true);
                            }
                        });

                    }
                    for (contador = 0; contador < jogada; ++contador){
                        //AQUI EU PRECISO QUE O PROGRAMA PARE E ESPERE O USUARIO PRESSIONAR UM DOS BOTÕES E DEPENDENDO DO BOTAO PRESSIONADO A VARIAVEL tentativa RECEBA DETERMINADO VALOR!
                        if (tentativa == sequencia[contador]){
                            acertos++;
                        } else {
                            gameOver();
                        }
                    }
                }
            }
        }).start();
    }
    
asked by anonymous 12.06.2015 / 05:00

2 answers

5

I think you put the same onClick in all of you right?

An output is you compare by the id of the component that sends the "clicked", like this:

public void cliqueGenerico(View v) {
    int tentativa;
    switch (v.getId()) {
        case R.id.botaoUm:
            tentativa = 0;
            break;
        case R.id.botaoDois:
            tentativa = 1;
            break;
        case R.id.botaoTres:
            tentativa = 2;
            break;
        case R.id.botaoQuatro:
            tentativa = 3;
            break;
    }

    /* ... seu código restante */
}
    
12.06.2015 / 05:07
-3

Case I: just put the "try" variable to receive the keyboard value. The loop will wait for the assignment.

Ex by Language:

Language C

scanf ("% d", & attempt);

Java language

Scanner scan = new Scanner (System.in); attempt = scan.nextLine ();

So the loop will execute until the value of the tentative variable is assigned.

Case 2:

do

} while (attempt! = 4);

    
12.06.2015 / 13:48