如何创建一个禁用其他按钮的功能

时间:2017-03-29 14:59:00

标签: java android button methods

我正在创建一个琐事游戏,让用户通过选择答案按钮来回答。我想创建一个功能,当用户点击按钮作为答案时,禁用其他按钮(答案选项)。目前,当用户连续按下多个按钮时,答案(敲击的答案)将用作下一个问题的答案,具体取决于多少次点击。我试图放置一个布尔标志来禁用其他按钮,但它将禁用转到下一个问题。

以下是我的按钮代码。

public void onClick(View v) {
    switch (v.getId()) {
        case (R.id.choice1_Button) :
            if (choice1.getText().equals(answer)) {
                correct = 1;
                sound(correct);
                score += 10;
                Toast.makeText(this, "Brilliant! The answer is correct", Toast.LENGTH_SHORT).show();
                scoreboard(score);
                getNextQuestion(counter);
            }else {
                correct = 0;
                sound(correct);
                Toast.makeText(this, "Nice try. The correct answer is "+answer, Toast.LENGTH_SHORT).show();
                if (score > 0) {
                    score = score - 3;
                }
                if (score < 0){
                    score = 0;
                }
                scoreboard(score);
                getNextQuestion(counter);
            }
            break;
        case (R.id.choice2_Button) :
            if (choice2.getText().equals(answer)) {
                correct = 1;
                sound(correct);
                score += 10;
                Toast.makeText(this, "Brilliant! The answer is correct", Toast.LENGTH_SHORT).show();
                scoreboard(score);
                getNextQuestion(counter);
            }else {
                correct = 0;
                sound(correct);
                Toast.makeText(this, "Nice try. The correct answer is "+answer, Toast.LENGTH_SHORT).show();
                if (score > 0) {
                    score = score - 3;
                }
                if (score < 0){
                    score = 0;
                }
                scoreboard(score);
                getNextQuestion(counter);
            }
            break;
        case (R.id.choice3_Button) :
            if (choice3.getText().equals(answer)) {
                correct = 1;
                sound(correct);
                score += 10;
                Toast.makeText(this, "Brilliant! The answer is correct", Toast.LENGTH_SHORT).show();
                scoreboard(score);
                getNextQuestion(counter);
            }else {
                correct = 0;
                sound(correct);
                Toast.makeText(this, "Nice try. The correct answer is "+answer, Toast.LENGTH_SHORT).show();
                if (score > 0) {
                    score = score - 3;
                }
                if (score < 0){
                    score = 0;
                }
                scoreboard(score);
                getNextQuestion(counter);
            }
            break;
    }
}

这是我的getNextQuestion方法

private void getNextQuestion(final int i) {
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (counter < 6) {
                setQuestion();
            }else {
                // if over 5 do this
                Intent intent = new Intent(Level6.this, NextLevel.class);
                Bundle a = new Bundle();
                a.putInt("score", score);
                a.putInt("level", 6);
                intent.putExtras(a);
                startActivity(intent);
                finish();
            }
        }
    },500);
}

这是我的setQuestion()方法

private void setQuestion() {
    emotion_View.setImageResource(questionsLib.getQuestions(counter));
    choice1.setText(questionsLib.getOptions1(counter));
    choice2.setText(questionsLib.getOptions2(counter));
    choice3.setText(questionsLib.getOptions3(counter));
    answer = questionsLib.getCorrectAnswer(counter);
    counter++;

}

1 个答案:

答案 0 :(得分:1)

制作禁用按钮组的方法,如下所示:

private void setQuestion(Boolean enable) {
    choice1.setEnabled(enable);
    choice2.setEnabled(enable);
    choice3.setEnabled(enable);
}
  1. 然后在getNextQuestion执行此方法之前设置按钮禁用(setQuestion(false)
  2. 在处理程序中按方法(setQuestion(true))修改下一个问题启用按钮后。
  3. 或者您可以在getNextQuestion方法中设置全部内容:

    private void getNextQuestion(final int i) {
        setQuestion(false); //disable buttons
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (counter < 6) {
                    setQuestion();
                }else {
                    // if over 5 do this
                    Intent intent = new Intent(Level6.this, NextLevel.class);
                    Bundle a = new Bundle();
                    a.putInt("score", score);
                    a.putInt("level", 6);
                    intent.putExtras(a);
                    startActivity(intent);
                    setQuestion(true); //enable buttons
                    finish();
                }
            }
        },500);
    }