我正在研究测验应用程序。我有一个普通的Java类来回答问题,它看起来像这样:
public class Question {
private String question;
private LinkedList<Answer> answers;
public String getQuestion(){
return this.question;
}
public LinkedList<Answer> getAnswers(){
return this.answers;
}
public Question(String question, LinkedList<Answer> answers){
this.question = question;
this.answers = answers;
}
public boolean checkIfCorrectAnswer(Answer answer){
if (null == answer)
return false;
for (Answer i : answers){
if (i == answer && answer.isCorrectAns())
return true;
}
return false;
}
}
和一个看起来像这样的答案类:
public class Answer {
private String answerTxt;
private boolean correctAns;
public String getAnswerTxt() {
return answerTxt;
}
public boolean isCorrectAns() {
return correctAns;
}
public Answer(String answerTxt) {
new Answer(answerTxt, false);
}
public Answer(String answerTxt, boolean correctAns){
this.answerTxt = answerTxt;
this.correctAns = correctAns;
}
}
现在,我想制作一个视图(xml文件),该视图将显示每个问题及其选项,我得出的结论是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".QuizActivity$PlaceholderFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textViewQuizTitle"
android:layout_marginTop="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textViewQuizQuestion"
android:layout_below="@+id/textViewQuizTitle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="35dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/textViewQuizQuestion"
android:layout_marginTop="20dp"
android:weightSum="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_weight="0.04" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.04" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3"
android:layout_gravity="center_horizontal"
android:layout_weight="0.04" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4"
android:layout_gravity="center_horizontal"
android:layout_weight="0.04" />
</LinearLayout>
</RelativeLayout>
我现在有两个问题-我不明白如何将简单的Java类附加到视图,另一个问题是问题之间的“导航”,当有人正确回答问题时,该问题应该再次加载视图,但加入了另一个“问题”。
我尝试从活动/片段教程中学习,但不幸的是,我检查过的那些都没有指出我的要求。
实际上,您为活动配置xml布局。了解活动,并以某种逻辑,只有在总是显示一个带有4个答案选项(按钮)的问题时,才可以使用xml布局。您可能应该使用诸如“单选按钮”或“微调按钮”之类的其他组件,而不是“ 4个按钮”,以获得更好的ui设计。您所需要的很简单,但是在开始任何操作之前,您必须了解“活动”和“视图”。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句