1

我有四个按钮,它们是布尔值——我想让它们在每次图像和其中的文本发生变化时都不同——即,一旦一个按钮为真,其他时候它就会为假。如何才能做到这一点?感谢你 !

@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);
}
4

2 回答 2

0

将按钮用作单选按钮是不明智的。两者都是不同的,有不同的目的,而且这个想法在语义上是错误的。

将您的单选按钮设置为普通按钮。

这是一个关于皮肤安卓的好指南

http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/

于 2012-05-29T21:54:55.403 回答
0

这很简单。只需像平常一样创建您的环境(图像和按钮)。为按钮分配适当的侦听器。然后创建一个新的 Random 对象。获取随机布尔值。如果布尔值 == true,则首先将true按钮添加到布局中,否则添加false按钮。

public View getEnvironment(Context context) {
        RelativeLayout rl = new RelativeLayout(context);

        ImageView image = new ImageView(context);
        image.setId(0);
        // Instantiate image (add image and listener is needed)
        Button truee = new Button(context);
        // Add event listener
        Button falsee = new Button(context);
        // Add event listener

        Random rand = new Random();

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(80, 80);
        lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL);

        image.setLayoutParams(lp);

        RelativeLayout.LayoutParams left = new RelativeLayout.LayoutParams(40, 80);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        lp.addRule(RelativeLayout.BELOW, 0);

        RelativeLayout.LayoutParams right = new RelativeLayout.LayoutParams(40, 80);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp.addRule(RelativeLayout.BELOW, 0);

        if (rand.nextBoolean()) {
            truee.setLayoutParams(left);
            falsee.setLayoutParams(right);
        } else {
            truee.setLayoutParams(right);
            falsee.setLayoutParams(left);
        }

        rl.addView(image);
        rl.addView(truee);
        rl.addView(falsee);

        return rl;
    }

编辑:

我没有意识到 OP 在发布之前要求使用CheckBox。我会留下这个以防万一有人想要它。

于 2012-05-29T19:40:19.690 回答