Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使Android中的布尔按钮随机?_Android_Button_Random_Boolean - Fatal编程技术网

如何使Android中的布尔按钮随机?

如何使Android中的布尔按钮随机?,android,button,random,boolean,Android,Button,Random,Boolean,我有四个按钮,它们是布尔型的-我想让它们在每次图像和其中的文本发生变化时都有所不同-也就是说,一旦一个按钮为真,其他时间它将为假。如何做到这一点?谢谢你 @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); startActivity(item.getIntent()); return true; } public void

我有四个按钮,它们是布尔型的-我想让它们在每次图像和其中的文本发生变化时都有所不同-也就是说,一旦一个按钮为真,其他时间它将为假。如何做到这一点?谢谢你

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

这很简单。只需像平常一样创建环境(图像和按钮)。为按钮指定合适的侦听器。然后创建一个新的随机对象。得到一个随机布尔值。如果布尔值==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在发帖前要求的是。我会把这个留着,以防有人需要。

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

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

下面是一个关于android蒙皮的好指南


你的问题不是很清楚。您想让您的按钮充当单选按钮吗?比如,用户触摸它们一次,它们就会被切换(从是到否,从否到是,从真到假,从假到真)。像这样?是的,如果我做对了,比如说按钮1是真的,下一次是假的,等等。对不起,我不太明白。但不是一个按钮总是正确的,而另一个按钮总是错误的吗?哦,你是说一个>?更像我有两个按钮,有时我希望按钮1是正确的,有时是错误的