如何更改多项选择的颜色以显示正确的答案

时间:2012-07-20 12:52:33

标签: android android-layout android-emulator

我正在做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; }

2 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分: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);

            }

其中 R.id.greenBackground - 您要在正确答案后面显示的可绘制的ID R.id.redBackground - 您想要在错误答案后面显示的可绘制的ID