测验应用,更改错误的按钮颜色和错误答案选择的正确答案,android

时间:2016-07-14 15:07:08

标签: java android button

我正在开发一个Android测验应用程序,我需要在选择错误答案时更改按钮的颜色。如果他们选择的按钮是 OR ,如果他们选择的按钮是正确的,我已经设置为已经改变了颜色。那么当选择了错误的答案时,用正确答案改变按钮颜色的最佳方法是什么?如果这是有道理的。基本上我需要有2个按钮,当你选择错误的答案时颜色会改变。红色表示他们选择的错误答案,绿色代表他们显然没有选择的正确答案。答案也被洗牌,所以不知道如何判断哪一个是正确的。

还试着把事情拼出来并给我一些例子,因为我只是弄湿了所有这些!它运行在一个片段中,如果有人有关于如何简化任何事情或修复坏代码的任何指示,请随时告诉我,因为我刚开始学习并需要我能得到的所有帮助。

这是我的 onActivityCreated

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    timesUp = (TextView)getActivity().findViewById(R.id.times_up);
    timesUp.setVisibility(View.INVISIBLE);

    Bundle c;
    c = getArguments();
    String setCategory = c.getString("category");

    QuizHelper db = new QuizHelper(this.getActivity()); // my question bank class
    quesList = db.getAllQuestions(setCategory);  // this will fetch all quetonall questions

    currentQ = quesList.get(qid);

    txtQuestion = (TextView)getActivity().findViewById(R.id.txtQuestion);
    // the textview in which the question will be displayed

    // the buttons,
    // the idea is to set the text of the buttons with the options from question bank
    button1 = (Button)getActivity().findViewById(R.id.button1);
    button2 = (Button)getActivity().findViewById(R.id.button2);
    button3 = (Button)getActivity().findViewById(R.id.button3);
    button4 = (Button)getActivity().findViewById(R.id.button4);

    //set buttons white
    button1.setBackgroundColor(Color.WHITE);
    button2.setBackgroundColor(Color.WHITE);
    button3.setBackgroundColor(Color.WHITE);
    button4.setBackgroundColor(Color.WHITE);

    // the textview in which score will be displayed
    scored = (TextView)getActivity().findViewById(R.id.score);
    scored.setText(getResources().getString(R.string.current_score, score));

    // the timer
    times = (TextView)getActivity().findViewById(R.id.timers);

    // method which will set the things up for our game
    allAnswers.clear();
    setQuestionView();
    times.setText(getResources().getString(R.string.start_time));

    // Start timer
    timer.start();

    // button click listeners
    // passing the button text to other method
    // to check whether the answer is correct or not
    // same for all three buttons
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button1);
        }
    });

    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button2);
        }
    });

    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button3);
        }
    });

    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            getAnswer(button4);
        }
    });

}

这是我的 getAnswer()(缩短了一点)

public void getAnswer(TextView AnswerString) {

    String option = AnswerString.getText().toString();

    //If timer is running stop and restart
    if(timer != null) {
        timer.cancel(); // Stop Timer
        timer.start(); // Start timer
    }

    if (currentQ.getANSWER().equals(option)) {

        // if conditions matches increase the int (score) by 1
        // and set the text of the score view
        score++;
        scored.setText(getResources().getString(R.string.current_score, score));
        AnswerString.setBackgroundColor(Color.GREEN);
    } else {

       //If answer is wrong change button colors

        AnswerString.setBackgroundColor(Color.RED);
        //WrongAnswer.setBackgroundColor(Color.GREEN);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                //Load ResultFragment()...                    

            }
        }, 1000);
        return;

    }
    if (qid < 25) {

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                // if questions are not over then do this
                currentQ = quesList.get(qid);
                allAnswers.clear();

                //set all buttons white for next question
                button1.setBackgroundColor(Color.WHITE);
                button2.setBackgroundColor(Color.WHITE);
                button3.setBackgroundColor(Color.WHITE);
                button4.setBackgroundColor(Color.WHITE);

                setQuestionView();

            }
        }, 1000);

    } else {

        // if "game over" (qid>25) do this
        //Load ResultFragment()...

}

最后我的 questionView()

private void setQuestionView() {

    allAnswers.add(currentQ.getOPTA());
    allAnswers.add(currentQ.getOPTB());
    allAnswers.add(currentQ.getOPTC());
    allAnswers.add(currentQ.getOPTD());

    Collections.shuffle(allAnswers);

    txtQuestion.setText(currentQ.getQUESTION());
    button1.setText(allAnswers.get(0));
    button2.setText(allAnswers.get(1));
    button3.setText(allAnswers.get(2));
    button4.setText(allAnswers.get(3));

    qid++;
}

感谢您帮助我!

1 个答案:

答案 0 :(得分:1)

修改

您可以通过以下方式获取按钮文字:

// for currentQ.getANSWER().equals(option)
String option = selectedButton.getText();

编辑前

getAnswer(Button selectedButton)错误答案字段

中调用此方法

从您的数据库中获取答案,并将其与所有按钮的getText()相匹配

// because you found out that selected answer/button was wrong
selectedButton.setBackgroundColor(Color.RED);

// take real answer from DB and store in String, probably you've already done this
String answerText = "take-answer-from-database";

选项1:

//match DB answer to selected answer, turn it green if it is correct
if(button1.getText().equals(answerText)){
    button1.setBackgroundColor(Color.GREEN);
} else if(button2.getText().equals(answerText)){
    button2.setBackgroundColor(Color.GREEN);
} else if(button3.getText().equals(answerText)){
    button3.setBackgroundColor(Color.GREEN);
} else if(button4.getText().equals(answerText)){
    button4.setBackgroundColor(Color.GREEN);
}

选项2:

// match DB answer to selected answer, turn it green if it is correct
ArrayList buttonList = new ArrayList();
buttonList.add(button1);
buttonList.add(button2);
buttonList.add(button3);
buttonList.add(button4);

for(Button button : buttonList){
    if(button.getText().equals(answerText)){
        button.setBackgroundColor(Color.GREEN);
        break;
    }
}

注意:如果您的getAnswer(Button)位于不同的班级,只需将您的按钮设为全局按钮public,然后使用其Class对象访问它们

如果您需要更多帮助,请告诉我