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

ここで示したものだけでなく、4 つのそれぞれに対してこれを行う必要があることに注意してください。

于 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 に答える