【问题标题】:How to make boolean buttons in Android random?如何在Android中随机制作布尔按钮?
【发布时间】: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


【解决方案1】:

这很简单。只需像平常一样创建您的环境(图像和按钮)。为按钮分配适当的侦听器。然后创建一个新的 Random 对象。获取随机布尔值。如果 boolean == 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。我会留下这个以防万一有人想要它。

【讨论】:

  • 抱歉,我不太明白。但是,难道不是一个按钮始终为真而另一个按钮始终为假吗?
  • 更像我有 2 个按钮,有时我希望按钮 1 为真,有时为假
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2012-01-15
    相关资源
    最近更新 更多