[英]Change the textview text color when button is clicked in android
I have a code for a Quiz Game in Android Studio. 我在Android Studio中有一个问答游戏的代码。 I need to change the color of text in textview when my answer is correct, but in my code the button color change when the question is the next. 当我的答案正确时,我需要在textview中更改文本的颜色,但是在我的代码中,下一个问题时按钮的颜色就会更改。 I click to the correct answer but the color put in the next question. 我单击正确的答案,但颜色在下一个问题中。 Can you help me? 你能帮助我吗?
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
}
what does the acierto.start();
acierto.start();
是什么acierto.start();
do? 做?
i think you should put respuesta1.setBackgroundColor(Color.parseColor("#76FF03"));
我认为你应该把respuesta1.setBackgroundColor(Color.parseColor("#76FF03"));
before that. 在那之前。
Can you put more code in the question. 您能否在问题中添加更多代码。 Assuming respuestaCorrecta
is of type String and respuesta1
is a radioButton with choice on it. 假设respuestaCorrecta
是String类型,而respuesta1
是带有选择的单选按钮。 respuesta1.getText()
will give you CharSequence
, Can you try String choiceSelectedByUser = respuesta1.getText().toString(); if(choiceSelectedByUser == respuestaCorrecta){
respuesta1.getText()
将为您提供CharSequence
,您可以尝试使用String choiceSelectedByUser = respuesta1.getText().toString(); if(choiceSelectedByUser == respuestaCorrecta){
String choiceSelectedByUser = respuesta1.getText().toString(); if(choiceSelectedByUser == respuestaCorrecta){
assuming the respuestaCorrecta
matches the correct answer's case
(case sensitivity) String choiceSelectedByUser = respuesta1.getText().toString(); if(choiceSelectedByUser == respuestaCorrecta){
假设respuestaCorrecta
匹配正确答案的case
(区分大小写)
Found the Solution after test and Debug . 经过测试和调试后找到解决方案。
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();
}
And also use .equals()
instead of ==
to compare Strings 并且还使用.equals()
代替==
来比较字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.