android多项选择测验应用程序

时间:2017-03-06 16:03:40

标签: android

你好,我是Android编程的新手我已经使用数组创建了一个Android多项选择测验应用程序。问题是当所有问题都在最后完成时我想要一个新的活动来,它显示计算结果。如何和我将为其编写代码Plz帮助

public class QuestionLibrary {

private String mQuestions[] = {
        "which number comes in the following series ? 10 50 250 1250__?__",
        "Free is to imprison as Forgive is to_____?",
        "My Mother is Sister of your Brother,what relation m i to you?",
        "Spot out the stranger in the following?"
};

private String mChoices[][] = {
        {"1350","6250","6500","1550"},
        {"Accuse","Accept","Condemn","Punish"},
        {"Uncle","Brother","Cousin","Nephew"},
        {"Atheist","Godly","Holy","Pious"}

};
private String mCorrectAnswers[] = {
        "6250",
        "Punish",
        "Cousin",
        "Atheist"
};
public String getQuestion(int a) {
    String question = mQuestions[a];
    return question;
}
public String getChoice1(int a) {
    String choice0 = mChoices[a][0];
    return choice0;
}
public String getChoice2(int a) {
    String choice1 = mChoices[a][1];
    return choice1;
}
public String getChoice3(int a) {
    String choice2 = mChoices[a][2];
    return choice2;
}
public String getChoice4(int a) {
    String choice3 = mChoices[a][3];
    return choice3;
}
public String getCorrectAnswer(int a) {
    String answer = mCorrectAnswers[a];
    return answer;
}

这是主要活动

mScoreView = (TextView) findViewById(R.id.score);
    mQuestionView = (TextView) findViewById(R.id.question);
    mButtonChoice1 = (Button) findViewById(R.id.choice1);
    mButtonChoice2 = (Button) findViewById(R.id.choice2);
    mButtonChoice3 = (Button) findViewById(R.id.choice3);
    mButtonChoice4 = (Button) findViewById(R.id.choice4);

    updateQuestion();

    mButtonChoice1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mButtonChoice1.getText()== mAnswer)
            {

                mScore+=1;
                updateScore(mScore);
                updateQuestion();
                Toast.makeText(MainActivity.this,"Correct",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(MainActivity.this,"Wrong",Toast.LENGTH_SHORT).show();

                updateQuestion();
            }
        }
    });
    mButtonChoice2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mButtonChoice2.getText()== mAnswer)
            {

                mScore+=1;
                updateScore(mScore);
                updateQuestion();
                Toast.makeText(MainActivity.this,"Correct",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(MainActivity.this,"Wrong",Toast.LENGTH_SHORT).show();

                updateQuestion();
            }
        }
    });
    mButtonChoice3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mButtonChoice3.getText()== mAnswer)
            {

                mScore+=1;
                updateScore(mScore);
                updateQuestion();
                Toast.makeText(MainActivity.this,"Correct",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(MainActivity.this,"Wrong",Toast.LENGTH_SHORT).show();

                updateQuestion();
            }
        }
    });
    mButtonChoice4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(mButtonChoice4.getText()== mAnswer)
            {

                mScore+=1;
                updateScore(mScore);
                updateQuestion();
                Toast.makeText(MainActivity.this,"Correct",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(MainActivity.this,"Wrong",Toast.LENGTH_SHORT).show();

                updateQuestion();
            }
        }
    });
}
private void updateQuestion()
{
    mQuestionView.setText(questionLibrary.getQuestion(mQuestionNumber));
    mButtonChoice1.setText(questionLibrary.getChoice1(mQuestionNumber));
    mButtonChoice2.setText(questionLibrary.getChoice2(mQuestionNumber));
    mButtonChoice3.setText(questionLibrary.getChoice3(mQuestionNumber));
    mButtonChoice4.setText(questionLibrary.getChoice4(mQuestionNumber));
    mAnswer = questionLibrary.getCorrectAnswer(mQuestionNumber);
    mQuestionNumber++;
}
private void updateScore(int point)
{
    mScoreView.setText(""+mScore);
}

1 个答案:

答案 0 :(得分:0)

在updateQuestion方法中编写以下代码。

if(mQuestionNumber>3)
{
Intent resultIntent = new Intent (this, ResultActivity.class);
resultIntent.putExtra('SCORE',mScore);
startActivity(resultIntent);
}

在“结果”活动中获取额外值并通过文本视图显示