0
I am developing an app that randomly generates an addition account and generates 4 values as possible answers and that are placed in 4 buttons (also in a random way).
I would like to know how I do, 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 9 more 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++;
}
}
});
}
What do you already have? Where exactly are you having problems? Is it with the color change? Is it with the deactivation of the other buttons? Is it with the algorithm? Break your problem in stages and try to solve one thing at a time. As it stands, the question is very wide.
– Pablo Almeida
Thanks for the tip, well let’s go for the first problem, I have an application that will repeat 10 times addition questions and that each time will generate 4 answers, I can change the color of the button the first time by clicking on the "answer", but already the second time onwards, the button gets the color of what I clicked earlier. How do I fix it?
– Christian Gomes da Silva
You can paste here the code snippet where you change the button color to analyze?
– Gustavo Bitencourt
I put, thanks!
– Christian Gomes da Silva