gpt4 book ai didi

java - Android按钮文字颜色不变

转载 作者:行者123 更新时间:2023-11-29 20:48:03 26 4
gpt4 key购买 nike

我正在开发一个有问题和答案选项的测验应用程序。单击错误答案(按钮)时,该特定按钮的文本颜色会变为红色,因为它应该如此。但是,当单击右键时,该特定按钮的文本颜色应变为绿色,稍等片刻,然后继续下一个问题。

我花了几天时间试图弄清楚为什么它不起作用,用谷歌搜索答案,但我仍然无法让它起作用。

我试过使用 Handlers 和 Runnables,但还是不行。

我已经发布了下面 Activity 的完整代码。

请帮忙!

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question_layout);

Intent i = getIntent();
subject = (Subject) i.getSerializableExtra(Globals.SUBJECT);
questions = subject.getQuestions();
subjectId = subject.getSubjectId();
appPreferences = new AppPreferences();

livesLeft = 3;
index = 0;
maxQuestions = questions.size();
maxIndex = maxQuestions - 1;

progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(maxQuestions);

answer1 = (Button) findViewById(R.id.buttonAnswer1);
answer2 = (Button) findViewById(R.id.buttonAnswer2);

setQuestionsAndAnswers(index);

TextView textView = (TextView) findViewById(R.id.textViewSubjectName);
textView.setText(subject.getSubjectName());
}

private void setQuestionsAndAnswers(int index)
{
currentQuestion = questions.get(index);

// Set question
TextView textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
textViewQuestion.setText(currentQuestion.getQuestion());

// Set correct answer
correctAnswer = currentQuestion.getCorrectAnswer();
sourceUrl = currentQuestion.getSourceUrl();
sourceText = currentQuestion.getSourceText();

// Set answer #1
initializeButton(answer1, currentQuestion.getAnswer1());

// Set answer #2
initializeButton(answer2, currentQuestion.getAnswer2());
// Set source
TextView textViewSource = (TextView) findViewById(R.id.textViewSource);
textViewSource.setText(sourceText);
textViewSource.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

textViewSource.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sourceUrl));
startActivity(browserIntent);
}
});

// Update progress
updateProgress(index);
}

private void initializeButton(Button button, String answer)
{
button.setText(answer);
button.setTextColor(Color.WHITE);
button.setBackgroundColor(Color.TRANSPARENT);
button.setEnabled(true);

button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
onClickContent(view);
}
});
}

private void onClickContent(View view)
{
Context context = getApplicationContext();
final Button button = (Button) view;
String answer = button.getText().toString();

if (answer.equalsIgnoreCase(correctAnswer))
{
button.setTextColor(Color.GREEN);

try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

if (index == maxIndex)
{
appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);

playSound(Globals.SOUND_QUIZ_COMPLETED);

Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_CORRECT_ANSER);

// Go to next question
index++;
setQuestionsAndAnswers(index);
}

} else
{
if (livesLeft == 1)
{
playSound(Globals.SOUND_GAME_OVER);
Toast toast = Toast.makeText(context, "Game over", Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_WRONG_ANSWER);

button.setTextColor(Color.RED);
button.setEnabled(false);
livesLeft--;
TextView lives = (TextView) findViewById(R.id.textViewLives);
lives.setText("Lives: " + livesLeft);
}
}
}

private void updateProgress(int progress)
{
progressBar.setProgress(progress++);
}

private void playSound(int songId)
{
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), songId);
mp.start();
}

最佳答案

首先,Thread.sleep(1000); 在你的例子中运行在 MAIN 线程上,导致 MAIN 线程 hibernate ,不建议这样做。

您可以view.postDelayed延迟加载下一个问题,

private void onClickContent(View view)
{
Context context = getApplicationContext();
final Button button = (Button) view;
String answer = button.getText().toString();

if (answer.equalsIgnoreCase(correctAnswer))
{
button.setTextColor(Color.GREEN);

if (index == maxIndex)
{
appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);

playSound(Globals.SOUND_QUIZ_COMPLETED);

Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
toast.show();
finish();
} else
{
playSound(Globals.SOUND_CORRECT_ANSER);

// Go to next question
index++;
// Go to next question after 1000ms
view.postDelayed(new Runnable() {
public void run() {
setQuestionsAndAnswers(index);
}
}, 1000); //here delays 1000ms
}

}
...
}

关于java - Android按钮文字颜色不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29885684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com