在不使用不必要的活动的情况下构建Android测验

时间:2013-08-15 11:38:54

标签: android android-layout

我是Android编程新手,想要进行一个包含大约50个问题的简单测验。我的问题是,如果不创建50个单独的布局,java类和活动,我不确定如何做到这一点。我希望每个问题都不同,问题的格式是多项选择(A,B,C和D)。任何有用的建议? 感谢。

3 个答案:

答案 0 :(得分:2)

很容易!只需创建一个活动,然后在每次用户回答问题时更新其数据:

1)用问题

更改TextView

2)用答案改变按钮

/*updating question
*questions is the list of questions obviously
*answers is the list of lists:) When you take a list from answers 
*you get a list with 
*4 items - they are the answers for current question
*/

public void updateQuestion() {
    mQuestionTextView.setText(questions.get(currentQuestionNum));
    mButtonA.setText(answers.get(currentQuestionNum).get(0));
    mButtonB.setText(answers.get(currentQuestionNum).get(1));
    mButtonC.setText(answers.get(currentQuestionNum).get(2));
    mButtonD.setText(answers.get(currentQuestionNum).get(3));
}

EDIT:

public ArrayList<String> getAnswers(int questionsNumber) {
     return ArrayList<String> currentQuestionAnswers = answers.get(questionsNumber);
}

答案 1 :(得分:1)

在每个问题上,您都可以这样做:

TextView textview = (TextView) findViewById(R.id.question);
question.setText(current_question);

对于多项选择,如果您使用的是RadioGroup,则可以有4个与每个选项对应的文本视图,您可以根据问题修改文本。

要识别选择了哪个选项,请将其放在onClickListener

radioGroup = (RadioGroup) findViewById(R.id.radio);
btnSend = (Button) findViewById(R.id.btnSend);

btnSend.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

            // get selected radio button from radioGroup
        int selectedId = radioGroup.getCheckedRadioButtonId();

        // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);



    }

});

答案 2 :(得分:0)

PL。检查此链接...它还有测验的源代码。 http://www.developerfeed.com/android/tutorial/simple-quiz-game-andriod