我有一个用于Android Studio中的测验游戏的代码。当我的答案正确时,我需要在textview中更改文本的颜色,但是在我的代码中,下一个问题时按钮的颜色就会更改。我单击正确的答案,但颜色在下一个问题中。你能帮我吗?
respuesta1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(respuesta1.getText() == respuestaCorrecta){ //if the answer is correct
puntos += 3;
puntuacion.setText("Puntuacion: " +puntos);
Toast.makeText(MainActivity.this, acertado, Toast.LENGTH_SHORT).show();
acierto.start(); //this is de sound
respuesta1.setBackgroundColor(Color.parseColor("#76FF03")); //change color to green
try {
sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
} //wait for change to other question
if(numeroPregunta<9){
numeroPregunta+=1;
preguntaMostrar(numeroPregunta); //show the next question
}else if(numeroPregunta == 9){
pantallaImagen.putExtra("puntuacionActual", puntos);
startActivity(pantallaImagen); //go to the final activity
}
答案 0 :(得分:0)
acierto.start();
会做什么?
我认为您应该在此之前放respuesta1.setBackgroundColor(Color.parseColor("#76FF03"));
。
答案 1 :(得分:0)
您可以在问题中添加更多代码吗?假设respuestaCorrecta
是String类型,而respuesta1
是带有选择的单选按钮。 respuesta1.getText()
将给您CharSequence
,您可以尝试吗
String choiceSelectedByUser = respuesta1.getText().toString();
if(choiceSelectedByUser == respuestaCorrecta){
假设respuestaCorrecta
与正确答案的case
(区分大小写)匹配
答案 2 :(得分:0)
经过测试和调试后找到了解决方案。
if(respuesta1.getText().equals(respuestaCorrecta)){
final Handler handler = new Handler();
new Thread(new Runnable() {
public void run() {
handler.post(new Runnable() {
public void run() {
// This Code will execute Directly onClick
puntos += 3;
puntuacion.setText("Puntuacion: " +puntos);
Toast.makeText(MainActivity.this, acertado, Toast.LENGTH_SHORT).show();
acierto.start(); //this is de sound
respuesta1.setBackgroundColor(Color.parseColor("#76FF03"));
}
});
try {
// Sleep for 200 milliseconds.
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
// This code will execute after 800 milliseconds
if(numeroPregunta<9){
numeroPregunta+=1;
preguntaMostrar(numeroPregunta); //show the next question
}else if(numeroPregunta == 9){
pantallaImagen.putExtra("puntuacionActual", puntos);
startActivity(pantallaImagen); //go to the final activity
}
}
}).start();
}
还可以使用.equals()
代替==
来比较字符串