我正在开发一个应用程序并尝试使用单击更新按钮背景颜色。我想做的是,
1)等待0.5秒检查答案是否正确。如果答案正确,将按钮颜色更改为绿色,否则将其更改为红色。
2) 按钮颜色变化后,再等待 0.5 秒,然后调用函数更新问题。
这是我的 onClick 方法,
@Override
public void onClick(View v) {
if(totalQuestionsAsked <= 10){
if(score >= 10)
score = 10;
//Setting up button and image
final Button btnOne = (Button)findViewById(R.id.btn_one);
final Button btnTwo = (Button)findViewById(R.id.btn_two);
final Button btnThree = (Button)findViewById(R.id.btn_three);
final Button btnFour = (Button)findViewById(R.id.btn_four);
final ImageView flagImg = (ImageView)findViewById(R.id.flag_img);
final JSONArray country = getFilesArray()[0];
final JSONArray flag = getFilesArray()[1];
final View view = v;
switch (v.getId()){
case R.id.btn_one:
btnOne.setBackgroundColor(Color.parseColor("#00b0ff"));
break;
case R.id.btn_two:
btnTwo.setBackgroundColor(Color.parseColor("#00b0ff"));
break;
case R.id.btn_three:
btnThree.setBackgroundColor(Color.parseColor("#00b0ff"));
break;
case R.id.btn_four:
btnFour.setBackgroundColor(Color.parseColor("#00b0ff"));
break;
}
final Handler handler = new Handler();
final Runnable runnerTwo = new Runnable() {
@Override
public void run() {
if(totalQuestionsAsked <= 10){
createCountryElements(flag, country, btnOne, btnTwo, btnThree, btnFour, flagImg);
}else{
Toast.makeText(MainActivity.this, "Your total score is: " + String.valueOf(score),
Toast.LENGTH_LONG).show();
}
}
};
Runnable runnerOne = new Runnable() {
@Override
public void run() {
if(view.getTag().equals(flagImg.getTag())){
score++;
switch (view.getId()){
case R.id.btn_one:
setButtonGreen(R.id.btn_one);
break;
case R.id.btn_two:
setButtonGreen(R.id.btn_two);
break;
case R.id.btn_three:
setButtonGreen(R.id.btn_three);
break;
case R.id.btn_four:
setButtonGreen(R.id.btn_four);
break;
}
}else{
switch (view.getId()){
case R.id.btn_one:
setButtonRed(R.id.btn_one);
break;
case R.id.btn_two:
setButtonRed(R.id.btn_two);
break;
case R.id.btn_three:
setButtonRed(R.id.btn_three);
break;
case R.id.btn_four:
setButtonRed(R.id.btn_four);
break;
}
}
totalQuestionsAsked++;
handler.postDelayed(runnerTwo, 1000);
}
};
handler.postDelayed(runnerOne, 500);
}
}
所以它只是不改变按钮的背景颜色。其他一切工作正常。谁能告诉我我的错误在哪里?
感谢你们对我的帮助。:D