Android Question OnClick

0

hello a query the issue is like this I have an onclick that generates a number depending on the button that presses (from 1 to 4) and I have a handler that at a certain moment has to pause and wait for those buttons to be pressed and compare it with an array list that I have. I do not know how to call the onclick and that the program waits for the response of the buttons to continue I use boton1.callOnClick (); but there I use only one of the buttons and also does not ask me to press the button to continue

this is the handler

   public void iniciarTimer() {
    c=0;
    encender=true;
    n = 0;
    generar();
    final android.os.Handler handler = new android.os.Handler();
    new Thread(new Runnable() {
        public void run() {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    numero = list.get(n);
                    if (encender == true) {
                        if (finalizar == false) {
                            switch (numero) {
                                case 1:

                                    boton1.setBackgroundColor(Color.RED);
                                    boton3.setBackgroundResource(R.color.azul);
                                    boton2.setBackgroundResource(R.color.verde);
                                    boton4.setBackgroundResource(R.color.amarillo);
                                    break;
                                case 2:
                                    boton2.setBackgroundColor(Color.GREEN);
                                    boton3.setBackgroundResource(R.color.azul);
                                    boton1.setBackgroundResource(R.color.rojo);
                                    boton4.setBackgroundResource(R.color.amarillo);
                                    break;
                                case 3:
                                    boton3.setBackgroundColor(Color.BLUE);
                                    boton2.setBackgroundResource(R.color.verde);
                                    boton1.setBackgroundResource(R.color.rojo);
                                    boton4.setBackgroundResource(R.color.amarillo);
                                    break;
                                case 4:
                                    boton4.setBackgroundColor(Color.YELLOW);
                                    boton3.setBackgroundResource(R.color.azul);
                                    boton2.setBackgroundResource(R.color.verde);
                                    boton1.setBackgroundResource(R.color.rojo);
                                    break;
                            }
//condicion para que aumente n +1 despues de la primer pasada del handler, 
ya que el primer color lo prende en la segunda pasada
                            if (c != 0) {
                                n++;
                            }
                            c++;
//limite de puntos en el juego
                            if (n > 10) {
                                finalizar = true;
                                encender = false;
                                fin();
                            }
//en nivel dificil que valla aumentando la velocidad
                            dificil = nivel;
                            if (dificil == 750) {
                                nivel = (nivel - 10);
                            }
// una vez que se recorre la lista que entre en el if para comparar
                            if (n == list.size()) {
// recorrer la lista
                                for (a = (0); a < (list.size()); a++) {
// esperar un valor de p al presionar un boton.
                                    p = 0;
                                    if (p == 0) {
                                        a--;
                                    }
// comparar si el boton presionado coincide con el de la lista de no ser asi 
que finalize la activity
                                    if (p != 0) {
                                        if (list.get(a) != p) {
                                            encender = false;
                                            finalizar = true;
                                            fin();
                                        }
                                    }
                                }
// si todos los botones coiniciden inicia el timer de vuelta para generar 
otro numero y arrancar a recorrer de 0
                                encender = false;

                            }
                            if (encender == true) {
                                if (finalizar == false) {
                                    handler.postDelayed(this, nivel);
                                }
                            } else {
//suma el punto si todos los botones coinciden y reinicia el timer para 
generar otro numero y arrancar a recorrer de 0
                                sumar();
                                apagar();
                                iniciarTimer();
                            }
                        } else {
                            apagar();
                        }
                    }
                }
            }, 1000);

        }
    }).start();
}

this is Onclick (use onClickListener)

    public void onClick(View v) {
    switch (v.getId()) {
        case R.id.b1:
            p = 1;
            break;
        case R.id.b2:
            p = 2;
            break;
        case R.id.b3:
            p = 3;
            break;
        case R.id.b4:
            p = 4;
            break;
    }
}
    
asked by Houth 03.07.2017 в 04:27
source

0 answers