我有四个按钮,它们是布尔值——我想让它们在每次图像和其中的文本发生变化时都不同——即,一旦一个按钮为真,其他时候它就会为假。如何才能做到这一点?感谢你 !
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
startActivity(item.getIntent());
return true;
}
public void onNoButton(View v) {
handleAnswerAndShowNextQuestion(false);
}
public void onYesButton(View v) {
handleAnswerAndShowNextQuestion(true);
}
private void handleAnswerAndShowNextQuestion(boolean bAnswer) {
int curScore = mGameSettings.getInt(GAME_PREFERENCES_SCORE, 0);
int nextQuestionNumber = mGameSettings.getInt(GAME_PREFERENCES_CURRENT_QUESTION, 1) + 1;
Editor editor = mGameSettings.edit();
editor.putInt(GAME_PREFERENCES_CURRENT_QUESTION, nextQuestionNumber);
// Log the number of "yes" answers only
if (bAnswer == true) {
editor.putInt(GAME_PREFERENCES_SCORE, curScore + 1);
}
editor.commit();
if (mQuestions.containsKey(nextQuestionNumber) == false) {
// Load next batch
try {
loadQuestionBatch(nextQuestionNumber);
} catch (Exception e) {
Log.e(DEBUG_TAG, "Loading updated question batch failed", e);
}
}
if (mQuestions.containsKey(nextQuestionNumber) == true) {
// Update question text
TextSwitcher questionTextSwitcher = (TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setText(getQuestionText(nextQuestionNumber));
// Update question image
ImageSwitcher questionImageSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
Drawable image = getQuestionImageDrawable(nextQuestionNumber);
questionImageSwitcher.setImageDrawable(image);
} else {
// Tell the user we don't have any new questions at this time
handleNoQuestions();
}
}
private void handleNoQuestions() {
TextSwitcher questionTextSwitcher = (TextSwitcher) findViewById(R.id.TextSwitcher_QuestionText);
questionTextSwitcher.setText(getResources().getText(R.string.no_questions));
ImageSwitcher questionImageSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
questionImageSwitcher.setImageResource(R.drawable.noquestion);
// Disable yes button
Button yesButton = (Button) findViewById(R.id.Button_Yes);
yesButton.setEnabled(false);
// Disable no button
Button noButton = (Button) findViewById(R.id.Button_No);
noButton.setEnabled(false);
}