How to change color of multiple choice to show correct answer

早过忘川 提交于 2020-01-06 20:12:45

问题


Im doing a multiple choice apps with 4 choices. How to prompt the user of the correct answer if he pressed the incorrect answer (at the same time).Here is a sample of the code.

        //Question/Answer options listener
        optionOne.setOnClickListener(this);     //On First Option selection
        optionTwo.setOnClickListener(this);     //On Second Option selection
        optionThree.setOnClickListener(this);   //On Third Option selection
        optionFour.setOnClickListener(this);    //On Forth Option selection

        skip.setOnClickListener(this);          //On Question Skip      
        pause.setOnClickListener(this);         //On Game Pause
        end.setOnClickListener(this);           //On Game End

    }

    public void onClick(View v) {
        if(v.getId() == optionOne.getId()){
            onOptionSelected(optionOne.getText().toString());
        }else if(v.getId() == optionTwo.getId()){
            onOptionSelected(optionTwo.getText().toString());
        }else if(v.getId() == optionThree.getId()){
            onOptionSelected(optionThree.getText().toString());
        }else if(v.getId() == optionFour.getId()){
            onOptionSelected(optionFour.getText().toString());
        }else if(v.getId() == pause.getId()){   //True when Game is Paused 

    //When an option of a question is selected
    private void onOptionSelected(String option){
        if(!isGamePaused && !isGameEnded) { //true when game is being played
            ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
            if(option.equals(tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1))) {
                correct += 1;
                remainingTime = mySecondsPassed;
                totalPoints += remainingTime * pointsPerRemainingSecond; 
                totalPoints += pointsPerCorrectAnswer;
            }
            else{
                incorrect += 1;
                totalPoints -= pointsPerWrongAnswer;
            }

I need to insert something on this portion to show the correct answer.

else{
    incorrect += 1;
    totalPoints -= pointsPerWrongAnswer;
    selectedOptionTxt.setBackgroundColor(Color.RED); 

Here is my .plist
<question>
<key>Question</key>
<string>What is the ....</string>
<key>Options</key>
<array>
<string>option 1</string>
<string>option 2</string>
<string>option 3</string>
<string>option 4</string>
</array>
<key>Answer</key>
<integer>2</integer>
 </question>

Here is the other code

public ATriviaQuestion(){
    isThisQuestionAsked = false;
    answer = -1;
    answered = "";
}

public String GetQuestion()
{ return this.question; }

public void SetQuestion(String _question)
{ this.question=_question; }

public ArrayList<String> GetOptions()
{ return this.options; }

public void SetOptions(ArrayList<String> _options)
{ this.options = _options; }

public int GetAnswer()
{ return this.answer; }

public void SetAnswer(int _answer)
{ this.answer = _answer; }

回答1:


The easiest way that I can see is to alter your onOptionSelected method to take the TextView as a parameter instead of just the string, you'll still be able to pull the string out in the same manner inside the method as you do outside of it now. And you'll have the reference that you need to change the background color.

//When an option of a question is selected
private void onOptionSelected(TextView selectedOptionTxt){
String option = selectedOptionTxt.getText().toString();

if(!isGamePaused && !isGameEnded) { //true when game is being played
    ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
    if(option.equals(tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1))) {
        correct += 1;
        remainingTime = mySecondsPassed;
        totalPoints += remainingTime * pointsPerRemainingSecond; 
        totalPoints += pointsPerCorrectAnswer;  
        selectedOptionTxt.setBackgroundColor(Color.GREEN);
    }
    else{
        incorrect += 1;
        totalPoints -= pointsPerWrongAnswer;
        selectedOptionTxt.setBackgroundColor(Color.RED);

and then change where you are calling this method to pass the entire TextView instead of just the strings. like this:

onOptionSelected(optionOne);

note that you'll need to do this for each of the four, not just the one that I've shown here.




回答2:


try this :

        if(v.getId() == optionOne.getId()){
            onOptionSelected(optionOne.getText().toString(),v);
        }else if(v.getId() == optionTwo.getId()){
            onOptionSelected(optionTwo.getText().toString(),v);
        }else if(v.getId() == optionThree.getId()){
            onOptionSelected(optionThree.getText().toString(),v);
        }else if(v.getId() == optionFour.getId()){
            onOptionSelected(optionFour.getText().toString(),v);
        }else if(v.getId() == pause.getId())}

    private void onOptionSelected(String option,View v){
        if(!isGamePaused && !isGameEnded) { //true when game is being played
            ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
            if(option.equals(tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1))) {
                correct += 1;
                remainingTime = mySecondsPassed;
                totalPoints += remainingTime * pointsPerRemainingSecond; 
                totalPoints += pointsPerCorrectAnswer;  
                v.setBackgroundResource(R.id.greenBackground);
            }
            else{
                incorrect += 1;
                totalPoints -= pointsPerWrongAnswer;
                v.setBackgroundResource(R.id.redBackground);

            }

Where R.id.greenBackground - ID of a drawable which you want to display behind the correct answer R.id.redBackground - ID of a drawable which you want to display behind the wrong answer



来源:https://stackoverflow.com/questions/11579779/how-to-change-color-of-multiple-choice-to-show-correct-answer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!