如何更改多重选择的颜色来显示正确答案 [英] How to change color of multiple choice to show correct answer

查看:192
本文介绍了如何更改多重选择的颜色来显示正确答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im做选择题的应用程序有4个选择。如何提示正确答案的用户,如果他$ pssed了不正确的答案(同时)p $。这里是code的样本。

  //问题/答案选项监听器
        optionOne.setOnClickListener(本); //在第一个选项的选择
        optionTwo.setOnClickListener(本); //在第二个选项的选择
        optionThree.setOnClickListener(本); //在第三个选项的选择
        optionFour.setOnClickListener(本); //在第四个选项的选择        skip.setOnClickListener(本); //在跳过问题
        pause.setOnClickListener(本); //在比赛暂停
        end.setOnClickListener(本); //在游戏结束    }    公共无效的onClick(视图v){
        如果(v.getId()== optionOne.getId()){
            onOptionSelected(optionOne.getText()的toString());
        }否则如果(v.getId()== optionTwo.getId()){
            onOptionSelected(optionTwo.getText()的toString());
        }否则如果(v.getId()== optionThree.getId()){
            onOptionSelected(optionThree.getText()的toString());
        }否则如果(v.getId()== optionFour.getId()){
            onOptionSelected(optionFour.getText()的toString());
        }否则如果(v.getId()== pause.getId()){//真当游戏暂停    //当选择问题的一种选择
    私人无效onOptionSelected(字符串选项){
        如果(isGamePaused&安培;!&安培;!isGameEnded){//真当比赛正在进行
            ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
            如果(option.equals(tTQuestion.GetOptions()获得(tTQuestion.GetAnswer() - 1))){
                正确的+ = 1;
                REMAININGTIME = mySecondsPassed;
                totalPoints + = REMAININGTIME * pointsPerRemainingSecond;
                totalPoints + = pointsPerCorrectAnswer;
            }
            其他{
                不正确+ = 1;
                totalPoints - = pointsPerWrongAnswer;
            }

我需要插入一些关于这部分,以显示正确答案。

 其他{
    不正确+ = 1;
    totalPoints - = pointsPerWrongAnswer;
    selectedOptionTxt.setBackgroundColor(Color.RED);这里是我的.plist
<问题>
<密钥GT;问题< /键>
<串GT;什么是....< /串>
<键>选项< /键>
<阵列GT;
<串GT;选项1 LT; /串>
<串GT;选项2'; /串>
<串GT;选项3'; /串>
<串GT;选择4℃; /串>
< /阵列>
<密钥GT;应答和LT; /键>
&所述;整数→2&下; /整数GT;
 < /问题>

下面是其他code

 公共ATriviaQuestion(){
    isThisQuestionAsked = FALSE;
    回答= -1;
    回答=;
}公共字符串GetQuestion()
{返回this.question; }公共无效SetQuestion(字符串_question)
{this.question = _question; }公众的ArrayList<串GT; GetOptions()
{返回this.options; }公共无效SetOptions(ArrayList中<串GT; _options)
{this.options = _options; }公众诠释的getAnswer()
{返回this.answer; }公共无效SetAnswer(INT _answer)
{this.answer = _answer; }


解决方案

这是我所看到的最简单的方法就是改变你的onOptionSelected方法采取的TextView作为参数,而不是仅仅字符串,你仍然可以拉串出在方法中的相同方式你现在要做的这外面。你就会有你需要改变背景颜色的基准。

  //当选择问题的选项
私人无效onOptionSelected(TextView的selectedOptionTxt){
。字符串选项= selectedOptionTxt.getText()的toString();如果(isGamePaused&安培;!&安培;!isGameEnded){//真当比赛正在进行
    ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
    如果(option.equals(tTQuestion.GetOptions()获得(tTQuestion.GetAnswer() - 1))){
        正确的+ = 1;
        REMAININGTIME = mySecondsPassed;
        totalPoints + = REMAININGTIME * pointsPerRemainingSecond;
        totalPoints + = pointsPerCorrectAnswer;
        selectedOptionTxt.setBackgroundColor(Color.GREEN);
    }
    其他{
        不正确+ = 1;
        totalPoints - = pointsPerWrongAnswer;
        selectedOptionTxt.setBackgroundColor(Color.RED);

,然后改变你在哪里调用此方法来传递整个的TextView而不只是字符串。像这样的:

  onOptionSelected(optionOne);

请注意,你需要为每个四个都这样做,不只是我在这里显示的。

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

解决方案

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.

这篇关于如何更改多重选择的颜色来显示正确答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆