How to change the color of the button you pressed?

0

I'm developing an app that randomly generates an add-in account and generates 4 values as possible responses and which are placed on 4 buttons (also in a random way).

I would like to know how I do it, when I click on any of the buttons (the possible answer), only it change color and do not let select others (do not let others change color).

Note: This process of choice would be repeated another 9 times.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_realizar_contas);
        final TextView num1 = (TextView) findViewById(R.id.num1);
        final TextView num2 = (TextView) findViewById(R.id.num2);
        final Button op1 = (Button) findViewById(R.id.op1);
        final Button op2 = (Button) findViewById(R.id.op2);
        final Button op3 = (Button) findViewById(R.id.op3);
        final Button op4 = (Button) findViewById(R.id.op4);
        final Button avancar = (Button) findViewById(R.id.btn_avancar);

        esc = numeroDecisao();
        res1 = numeroAleatorio();
        res2 = numeroAleatorio();

        //Toast.makeText(getApplicationContext(), "Esc " + esc, Toast.LENGTH_SHORT).show();

        if (esc == 1) {
            val1 = res1 + res2;
            val2 = (numeroAleatorio() + numeroAleatorio()) + 1;
            val3 = (numeroAleatorio() + numeroAleatorio()) + 2;
            val4 = (numeroAleatorio() + numeroAleatorio()) + 3;

        } else if (esc == 2) {
            val1 = (numeroAleatorio() + numeroAleatorio()) + 3;
            val2 = res1 + res2;
            val3 = (numeroAleatorio() + numeroAleatorio()) + 7;
            val4 = (numeroAleatorio() + numeroAleatorio()) + 1;

        } else if (esc == 3) {
            val1 = (numeroAleatorio() + numeroAleatorio()) + 8;
            val2 = (numeroAleatorio() + numeroAleatorio()) + 9;
            val3 = res1 + res2;
            val4 = (numeroAleatorio() + numeroAleatorio()) + 10;
        } else if (esc == 4) {
            val1 = (numeroAleatorio() + numeroAleatorio()) + 7;
            val2 = (numeroAleatorio() + numeroAleatorio()) + 6;
            val3 = (numeroAleatorio() + numeroAleatorio()) + 5;
            val4 = res1 + res2;
        }

        num1.setText(Integer.toString(res1));
        num2.setText(Integer.toString(res2));
        op1.setText(Integer.toString(val1));
        op2.setText(Integer.toString(val2));
        op3.setText(Integer.toString(val3));
        op4.setText(Integer.toString(val4));

        op1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                op1.setBackgroundResource(R.drawable.botao_customizado);
                press = press + 1;
            }
        });


        //BOTÃO - AVANÇAR - Realizar a procdimento anterior 9 vezes
        avancar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cont = 0;
                //Toast.makeText(getApplicationContext(), "Acertos " + acertos, Toast.LENGTH_LONG).show();

                while (cont != 9) {
                    res1 = numeroAleatorio();
                    res2 = numeroAleatorio();
                    esc = numeroDecisao();

                    if (esc == 1) {
                        val1 = res1 + res2;
                        val2 = (numeroAleatorio() + numeroAleatorio()) + 9;
                        val3 = (numeroAleatorio() + numeroAleatorio()) + 3;
                        val4 = (numeroAleatorio() + numeroAleatorio()) + 1;

                    } else if (esc == 2) {
                        val1 = (numeroAleatorio() + numeroAleatorio()) + 4;
                        val2 = res1 + res2;
                        val3 = (numeroAleatorio() + numeroAleatorio()) + 5;
                        val4 = (numeroAleatorio() + numeroAleatorio()) + 7;

                    } else if (esc == 3) {
                        val1 = (numeroAleatorio() + numeroAleatorio()) + 8;
                        val2 = (numeroAleatorio() + numeroAleatorio()) + 9;
                        val3 = res1 + res2;
                        val4 = (numeroAleatorio() + numeroAleatorio()) + 10;
                    } else if (esc == 4) {
                        val1 = (numeroAleatorio() + numeroAleatorio()) + 10;
                        val2 = (numeroAleatorio() + numeroAleatorio()) + 3;
                        val3 = (numeroAleatorio() + numeroAleatorio()) + 1;
                        val4 = res1 + res2;
                    }

                    num1.setText(Integer.toString(res1));
                    num2.setText(Integer.toString(res2));
                    op1.setText(Integer.toString(val1));
                    op2.setText(Integer.toString(val2));
                    op3.setText(Integer.toString(val3));
                    op4.setText(Integer.toString(val4));
                    cont++;


                }
            }
        });
    }
    
asked by anonymous 16.10.2017 / 03:47

1 answer

1

You can try to use the setBackgroundColor function and enter a hexadecimal value of the color. opt1.setBackgroundColor(0xFFFFA500);

If you already know CSS, you can change the color by changing the last 6 values (FFA500) by the color of choice. There are several hexadecimal color tables on the internet.

For more information see an English post on the subject. Android Button color changing onClick?

    
17.10.2017 / 03:30