【发布时间】:2012-05-29 19:32:30
【问题描述】:
我有四个按钮,它们是布尔型的——我想让它们在每次图像和其中的文本发生变化时都不同——即,一旦一个按钮为真,其他时候它就会为假。如何才能做到这一点?谢谢你!
@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);
}
【问题讨论】:
-
你的问题不是很清楚。你想让你的按钮充当单选按钮吗?比如,用户触摸它们一次,它们就会被切换(从是到否,从否到是,从真到假,从假到真)。像这样?
-
是的,如果我做对了 - 例如一次 button1 为真,下一次为假,等等。谢谢
标签: android button random boolean