简体   繁体   English

测验应用程序,更改错误的按钮颜色并针对错误的答案选择纠正答案,Android

[英]Quiz app, change button color of wrong and correct answer on wrong answer selection, android

I am working on an android quiz app and I need to change the color of the buttons when a wrong answer is selected. 我正在使用android测验应用程序,当选择了错误的答案时,我需要更改按钮的颜色。 I have it setup to already change the color if the button they select is wrong OR if the button they select is right. 如果他们选择的按钮错误, 或者他们选择的按钮正确,我将其设置为已经更改了颜色。 So what is the best way to ALSO change the color of the button with the correct answer when a incorrect answer is selected? 那么,当选择了不正确的答案时,最好的方法是同时更改具有正确答案的按钮的颜色的最佳方法是什么? If that makes sense. 如果这样的话。 Basically I need to have 2 buttons that the color is changed when you select the wrong answer. 基本上,当您选择错误的答案时,我需要有2个按钮来更改颜色。 Red for the wrong answer they selected and green for the right answer that they obviously did not select. 红色代表他们选择的错误答案,绿色代表他们显然没有选择的正确答案。 Answers are shuffled as well so not sure how to tell which one is right. 答案也被打乱了,因此不确定如何判断哪个是正确的。

Also try to spell things out and give me examples as I am just getting my feet wet in all of this! 还尝试说明问题,并给我一些例子,因为我刚开始湿透所有这些! It is run in a fragment and if anyone has any pointers on how to streamline anything or fix bad code feel free to let me know as I am just starting to learn and need all the help I can get. 它运行在一个片段中,如果有人对如何精简任何内容或修复错误的代码有任何指示,请随时告诉我,因为我才刚刚开始学习并需要我所能获得的所有帮助。

Here is my onActivityCreated 这是我的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);
        }
    });

}

Here is my getAnswer() (shortened a bit) 这是我的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()...

}

and finally my questionView() 最后是我的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++;
}

Thanks for helping me out! 谢谢你的协助!

EDIT 编辑

You can get button text with : 您可以使用以下按钮获取按钮文字:

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

BEFORE EDIT 编辑之前

Call this from getAnswer(Button selectedButton) wrong answer field getAnswer(Button selectedButton)错误的答案字段中调用此getAnswer(Button selectedButton)

Get answer from your database and match it with getText() of all your buttons 从数据库中获取答案,并将其与所有按钮的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";

Option 1: 选项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);
}

Option 2: 选项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;
    }
}

Note: If your getAnswer(Button) is in different class, simply make your buttons global and public , Then, access them with their Class object 注意:如果您的getAnswer(Button)位于不同的类中,则只需将您的按钮设为global和public ,然后使用其Class对象访问它们

Let me know if you need more help 让我知道您是否需要更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM