【问题标题】:how to make a function that disable the other buttons如何制作禁用其他按钮的功能
【发布时间】:2017-08-23 03:48:27
【问题描述】:

我正在创建一个问答游戏,让用户通过选择答案按钮获得答案。当用户点击按钮作为答案时,我想制作一个禁用其他按钮(答案选择)的功能。目前,当用户连续按下多个按钮时,答案(点击答案)将用作下一个问题的答案,具体取决于点击次数。我曾尝试放置一个布尔标志来禁用其他按钮,但它将无法进入下一个问题。

这是我的按钮代码。

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++;

}

【问题讨论】:

    标签: java android button methods


    【解决方案1】:

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

    private void setQuestion(Boolean enable) {
        choice1.setEnabled(enable);
        choice2.setEnabled(enable);
        choice3.setEnabled(enable);
    }
    
    1. 然后在getNextQuestion之前执行这个方法设置按钮禁用(setQuestion(false))
    2. 在处理程序中通过方法 (setQuestion(true)) 设置下一个问题后启用按钮。

    或者你可以在你的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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-06
      • 2012-06-02
      • 2015-02-23
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 2015-06-20
      相关资源
      最近更新 更多