2

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

4

2 回答 2

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);
}
于 2013-08-15T11:41:58.647 回答
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);



    }

});
于 2013-08-15T13:14:56.703 回答