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