So, I have a game of questions and answers. Where each question has 4 answers (placed on the button labels). How do I get the text of the button clicked to compare to a String?
So, I have a game of questions and answers. Where each question has 4 answers (placed on the button labels). How do I get the text of the button clicked to compare to a String?
You must first fetch the XML element,
final Button btnResposta1 = (Button) findViewById(R.id.btnResposta1);
final Button btnResposta2 = (Button) findViewById(R.id.btnResposta2);
After implementing onClickListener
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
//faca um cast para Button
Button botao = (Button)view;
//pegue o texto
String respostaBotao = botao .getText().toString();
switch (view.getId()){
case (R.id.btnResposta1):
//codigo caso clicar resposta 1
break;
case (R.id.btnResposta2):
//codigo caso clicar resposta 2
break;
}
}
then link to the button listener.
btnResposta1.setOnClickListener(onClickListener);
btnResposta2.setOnClickListener(onClickListener);
I hope to have helped!