Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- TextView textView1, textView2, textView3, textView4;
- Random random = new Random();
- ArrayList<Integer> list = new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView1 = (TextView) findViewById(R.id.textView1);
- textView2 = (TextView) findViewById(R.id.textView2);
- textView3 = (TextView) findViewById(R.id.textView3);
- textView4 = (TextView) findViewById(R.id.textView4);
- textView1.setOnClickListener(this);
- textView2.setOnClickListener(this);
- textView3.setOnClickListener(this);
- textView4.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- list.add(R.color.colorRed);
- list.add(R.color.colorPink);
- list.add(R.color.colorPurple);
- list.add(R.color.colorIndigo);
- list.add(R.color.colorBlue);
- list.add(R.color.colorCyan);
- list.add(R.color.colorTeal);
- list.add(R.color.colorLime);
- list.add(R.color.colorYellow);
- list.add(R.color.colorDeepOrange);
- int color1 = random.nextInt(10);
- int color2 = random.nextInt(10);
- int color3 = random.nextInt(10);
- int color4 = random.nextInt(10);
- while (true) {
- if (color1 != color2) {
- break;
- } else {
- color2 = random.nextInt(10);
- }
- }
- while (true) {
- if (color3 != color2 && color3 != color1) {
- break;
- } else {
- color3 = random.nextInt(10);
- }
- }
- while (true) {
- if (color4 != color2 && color4 != color3 && color4 != color1) {
- break;
- } else {
- color4 = random.nextInt(10);
- }
- }
- if (v == textView1) {
- textView2.setBackgroundColor(getResources().getColor(list.get(color2)));
- textView3.setBackgroundColor(getResources().getColor(list.get(color3)));
- textView4.setBackgroundColor(getResources().getColor(list.get(color4)));
- }
- if (v == textView2) {
- textView1.setBackgroundColor(getResources().getColor(list.get(color1)));
- textView3.setBackgroundColor(getResources().getColor(list.get(color3)));
- textView4.setBackgroundColor(getResources().getColor(list.get(color4)));
- }
- if (v == textView3) {
- textView1.setBackgroundColor(getResources().getColor(list.get(color1)));
- textView2.setBackgroundColor(getResources().getColor(list.get(color2)));
- textView4.setBackgroundColor(getResources().getColor(list.get(color4)));
- }
- if (v == textView4) {
- textView1.setBackgroundColor(getResources().getColor(list.get(color1)));
- textView2.setBackgroundColor(getResources().getColor(list.get(color2)));
- textView3.setBackgroundColor(getResources().getColor(list.get(color3)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement