0

我正在做一个有 4 个选择的多项选择应用程序。如果用户(同时)按下了错误的答案,如何提示用户正确答案。这里是一个代码示例。

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

我需要在这部分插入一些东西来显示正确的答案。

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>

这是另一个代码

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

2 回答 2

0

我能看到的最简单的方法是更改​​您的 onOptionSelected 方法以将 TextView 作为参数而不仅仅是字符串,您仍然可以在方法内部以与在方法外部相同的方式拉出字符串现在。您将获得更改背景颜色所需的参考。

//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);

然后更改调用此方法的位置以传递整个 TextView 而不仅仅是字符串。像这样:

onOptionSelected(optionOne);

请注意,您需要为这四个中的每一个都执行此操作,而不仅仅是我在这里展示的那个。

于 2012-07-20T14:34:30.453 回答
0

试试这个 :

        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- 你想在正确答案后面显示 R.id.redBackground的可绘制对象的 ID - 要在错误答案后面显示的可绘制对象的 ID

于 2012-07-20T14:36:21.410 回答