0

Necesito crear un metodo para que, al hacer click en un boton, se guarde su id y el color de fondo, y al presionar click en un segundo boton, se compruebe si es la segunda vez que se presiona un boton. En caso de que sea la segunda vez, se realiza el intercambio de los colores de fondo entre cada boton mutuamente. Yo tengo pensado algo asi:

PD: aclaro que aun no tengo muy en claro el uso del OnClickListener, apenas estoy empezando a aprender android D:

public class MainActivity extends AppCompatActivity {

Button boton1, boton2;
Drawable color1,color2;
int [] colores;
int [] coloresRandom;
int clicks = 0;
TableLayout tablero;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    colores = getResources().getIntArray(R.array.todos);
    coloresRandom = getResources().getIntArray(R.array.todosRandom);
    for (int i = 0; i < 25; i++) {
        Random random = new Random();
        int posRandom = random.nextInt(coloresRandom.length);
        int temp = coloresRandom[i];
        coloresRandom[i] = coloresRandom[posRandom];
        coloresRandom[posRandom] = temp;
    }
    for (int i = 0; i < 25; i++) {
        String buttonid = ("botona" + (i + 1));
        int resID = getResources().getIdentifier(buttonid, "id", getPackageName());
        Button boton = ((Button) findViewById(resID));
        boton.setBackgroundColor(colores[i]);
    }
    for (int i = 0; i < 25; i++) {
        String buttonid = ("boton" + (i + 1));
        int resID = getResources().getIdentifier(buttonid, "id", getPackageName());
        Button boton = ((Button) findViewById(resID));
        boton.setBackgroundColor(coloresRandom[i]);
    }
    tablero = findViewById(R.id.tablero1);
    tablero.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity.this.cambiaColor(v);
        }
    });
}

public void cambiaColor(View v){
    clicks = clicks++;
    if (clicks == 1){
        boton1 = findViewById(v.getId());
        color1 = boton1.getBackground();
    }
    if (clicks == 2){
        boton2 = findViewById(v.getId());
        color2 = boton2.getBackground();
        boton1.setBackground(color2);
        boton2.setBackground(color1);
    }
    if (clicks == 2){
        clicks = 0;
    }
}

1 respuesta 1

0

Prueba con este ejemplo que te arme, si tienes dudas comentalas para que te vayas dando una idea, espero sea lo que buscas hacer.

public class MainActivity extends AppCompatActivity {

    private Button boton = null;
    private boolean activo = true;
    private int idBoton = 0;

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

        final Context context = this;
        boton = findViewById(R.id.button);
        boton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(activo){
                    idBoton = v.getId();
                    boton.setBackgroundColor(ContextCompat.getColor(context, R.color.colorAccent));
                }
                else {
                    idBoton = v.getId();
                    boton.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
                }
                activo = !activo;
            }
        });
    }
}

Tu Respuesta

Al pulsar en “Publica tu respuesta”, muestras tu consentimiento a nuestros términos de servicio, política de privacidad y política de cookies

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.