【问题标题】:Show correct answer in multiple choice app [duplicate]在多项选择应用程序中显示正确答案[重复]
【发布时间】:2013-03-08 23:31:19
【问题描述】:

我正在做一个有 4 个选项的多项选择应用程序。我已经下载了一个代码。但是如果他的答案是错误答案(同时)如何提示用户正确答案。这是代码示例。正确答案是绿色。错误答案是红色的。

    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

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

//When an option of a question is selected
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;
            v.setBackgroundColor(Color.GREEN);

        }
        else{
            incorrect += 1;
            totalPoints -= pointsPerWrongAnswer;
        }

如果用户回答不正确但我不知道怎么做,我需要在这部分插入一个代码以在绿色背景中显示正确答案。

else{
incorrect += 1;
totalPoints -= pointsPerWrongAnswer;

数据库问题在 .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>

这是我的其他代码

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

【问题讨论】:

  • 您需要更好地描述您想要什么以及您的问题是什么。 “我需要在这里插入一些东西......”并没有告诉我们任何事情。你想如何显示正确答案(TextViewToastAlert...)?
  • @codeMagic 我想获取当前正确答案的位置,并在用户答案不正确时将其背景更改为绿色(同时显示它们)。我认为代码必须插入到 else 语句中。

标签: android


【解决方案1】:

我无法准确了解这里的内容,但您可以循环查看您的 options,直到文本等于 tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1)(这似乎是您获得正确答案的方式)然后设置 View' s 背景颜色为绿色。

【讨论】:

  • 谢谢。代码已经在工作了。这是当前的情况。如果我在第 1 次、第 2 次或第 3 次尝试中选择了错误的答案,在我最终得到正确答案之前,所有错误答案的背景都会变为红色。我想要做的是“如果用户在他的第一次尝试中不正确,正确的答案也将同时显示为绿色”。不再进行第二次或第三次尝试。
猜你喜欢
  • 1970-01-01
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2018-11-30
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多