Android测验中的多个答案

时间:2018-10-14 21:09:24

标签: java android xml

我正在尝试为Android创建随机的多项选择测验。我想显示随机问题和选项的相应答案。

两个问题:

  1. 如何显示问题和选择,
  2. 如何使用户能够选择一个,两个或多个正确选项(答案)?

这是代码:

家庭(主要)代码:

public class Home extends AppCompatActivity {

    private QuestionLibrary questionLibrary = new QuestionLibrary();
    private TextView mScoreView;
    private TextView mScoreView1;
    private TextView mQuestionNr;
    private TextView mQuestionView;
    private ImageView imageView;
    private int currentQuestion = 0;
    RadioGroup answerRadioGroup

    private String mAnswer;
    private int mScore = 0;
    private int mQuestionNumber = 0;
    public int selectedId = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);

        mScoreView =(TextView)findViewById(R.id.score);
        mScoreView1=(TextView)findViewById(R.id.score1);
        mQuestionNr=(TextView)findViewById(R.id.questionNr);
        mQuestionView = (TextView)findViewById(R.id.question);

        answerRadioGroup = (RadioGroup) findViewById(R.id.answers);

        updateQuestion();

        answerRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId > 0) {
                    Question qna = questionLibrary.GetQuestion(1);

                }
            }
        });

        imageView = (ImageView)findViewById(R.id.profile);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openProfile();
            }
        });
    }

    private void openProfile() {
        Intent intent = new Intent(this, SavedProfile.class);
        startActivity(intent);
    }

    private void updateQuestion(){
        if(mQuestionNumber<questionLibrary.allQuestions.size()) {

            Question qna = questionLibrary.GetQuestion(currentQuestion);
            mQuestionView.setText(qna.pytja);

            mQuestionNumber++;
            mQuestionNr.setText(mQuestionNumber + "");
        }
        else {
            Toast.makeText(Home.this, "It was the last question", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this,SavedProfile.class);
            intent.putExtra("score",mScore);
            startActivity(intent);
        }
    }
}

问题代码:

public class Question {
    public int Id;
    public String pytja;
    public List<Answer> answers;
}

QuestionLibrary

public class QuestionLibrary {

    public static  List<Question> allQuestions;

    public QuestionLibrary()
    {
        Question question = new Question();
        List<Answer> answers = new ArrayList<Answer>();
        question.Id = 1;
        question.pytja = "Test1";
        question.answers.add(new Answer("choice 1",false,1));
        question.answers.add(new Answer("choice 1",true,2));
        question.answers.add(new Answer("choice 1",true,3));
        question.answers.add(new Answer("choice 1",false,4));
        allQuestions.add(question);

        question.Id = 2;
        question.pytja = "Test2";
        question.answers.add(new Answer("pytja2",false,1));
        question.answers.add(new Answer("pytja2",true,2));
        question.answers.add(new Answer("pytja2",true,3));
        question.answers.add(new Answer("pytja2",false,4));
        allQuestions.add(question);

        question.Id = 3;
        question.pytja = "Test3";
        question.answers.add(new Answer("pytja3",false,1));
        question.answers.add(new Answer("pytja3",true,2));
        question.answers.add(new Answer("pytja3",true,3));
        question.answers.add(new Answer("pytja3",false,4));
        allQuestions.add(question);
    }
    public static Question GetQuestion(int id)
    {
        for (Question q: allQuestions) {
            if(q.Id == id)
                return q;
        }
        return  null;
    }
    public static boolean IsCorrectAnswer(List<Answer> answersSubmitted, int questionId)
    {
        int correctSubmitted = 0;
        int allCorrected = 0;
        Question question = GetQuestion(questionId);
        for (Answer questionAnswer:question.answers) {
            if(questionAnswer.isCorrect)
            {
                allCorrected = allCorrected + 1;
            }
        }
        for (Answer answerSubmitted: answersSubmitted) {
            for (Answer questionAnswer: question.answers) {
                if(questionAnswer.Id == answerSubmitted.Id && questionAnswer.isCorrect)
                {
                    correctSubmitted = correctSubmitted + 1;
                }
            }
        }
        if(correctSubmitted == allCorrected)
            return  true;
        else
            return  false;
    }

}

0 个答案:

没有答案