Código:
import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.Random; public class Game extends AppCompatActivity { Button botones[]; boolean rojo,amarillo,verde,azul; Random random; int repeticiones=6; int colores[]; Handler handler; int a=0; //Rojo = 0; Amarillo = 1; Verde = 2; Azul = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game); random = new Random(); handler = new Handler(); botones = new Button[4]; colores = new int[repeticiones]; botones[0] = (Button) findViewById(R.id.btnrojo); botones[1] = (Button) findViewById(R.id.btnverde); botones[2] = (Button) findViewById(R.id.btnamarillo); botones[3] = (Button) findViewById(R.id.btnazul); for (a=0; a<botones.length; a++){ botones[a].setClickable(false); switch (colores[a]) { case 0: botones[0].setBackgroundResource(R.drawable.ro); break; case 1: botones[1].setBackgroundResource(R.drawable.ve); break; case 2: botones[2].setBackgroundResource(R.drawable.am); break; case 3: botones[3].setBackgroundResource(R.drawable.az); break; default: //do nothing break; } handler.postDelayed(new Runnable() { @Override public void run() { switch (colores[a]) { case 0: botones[0].setBackgroundResource(R.drawable.ro3); break; case 1: botones[1].setBackgroundResource(R.drawable.ve3); break; case 2: botones[2].setBackgroundResource(R.drawable.am3); break; case 3: botones[3].setBackgroundResource(R.drawable.az3); break; default: //do nothing break; } } },800); } juego(); } public void juego(){ for (int i=0; i<repeticiones; i++){ int color = random.nextInt(4); colores[i]=color; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_game, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }