简体   繁体   English

Android:一个活动中的多个视图

[英]Android: multiple views in one activity

I am new to android, and I am working on a Quiz app with a set of questions, each question has a question(text) and 4 images(imageButton), when user select the correct image, then they go to next question. 我是Android新手,我正在开发一个带有一组问题的测验应用程序,每个问题都有一个问题(文本)和4个图像(imageButton),当用户选择正确的图像时,他们会转到下一个问题。 So from question to question, should I: 所以从问题到问题,我应该:

  1. call setContentView() multiple times in one activity? 在一个活动中多次调用setContentView()? (layout is same for all questions, only the question text and image changes) (所有问题的布局都相同,只有问题文字和图片发生变化)
  2. or add multiple layout in xml, and at runtime, set visibility of each layout? 或者在xml中添加多个布局,并在运行时设置每个布局的可见性? (but by doing so, I received warnings of too many views in one xml file) (但通过这样做,我在一个xml文件中收到了太多视图的警告)
  3. any other better suggestions? 还有其他更好的建议吗

Thanks for your reply! 感谢您的回复!

My suggestion populate the question dynamically... 我的建议动态填充问题......

From your Perspective do like this 从你的角度来看,这样做

1.Take xml with Question text and 4 images. 1.使用问题文本和4张图像获取xml。 2. Get the Ids of question text(TextView), and 4 imageview's. 2.获取问题文本(TextView)的ID和4个imageview。 3.When ever the event trigger(ie next question) change the question text and Answer images.. 3.当事件触发(即下一个问题)时,更改问题文本和答案图像..

Try this simple logic 试试这个简单的逻辑

   int question=0;
ImageView answer1,answer2,answe3,answer4;
int answerids[]={R.id.answer1,R.id.answer1,R.id.answer1,R.id.answer1};
ImageView[] answerimages={answer1,answer2,answer3,answer4};
String questions[]={"Question1","Question2","...."};
int images[][]={{R.drawable.answer1,R.drawable.answer2,R.drawable.answer3,R.drawable.answer4},{second question drawables},{Third question drawables}...};

In onCreate do like this 在onCreate做这样的

TextView question=findViewById(R.id.question);
    for(int i=0;i<answerids.length;i++)
    {
        answerimages[i]=(ImageView)findViewById(answerids[i]);
        answerimages[i].setImageResource(images[question][i]);
        answerimages[i].setOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                question+=1;
            nextqueston();

            }
        });

To populate next question text and images.. 填充下一个问题文本和图像..

 public void nextqueston()
        {
            question.setText(questions[queston]);
            for(int i=0;i<answerids.length;i++)
            {
                answerimages[i].setImageResource(images[question][i]);
            }
        }

Just keep the same Activity open, when one question is done, set the question text on the TextView programmatically, same for the ImageViews. 只需保持相同的Activity打开,当完成一个问题时,以编程方式在TextView上设置问题文本,对于ImageViews也是如此。 All in the same layout. 所有都在相同的布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM