【发布时间】:2014-07-20 14:14:48
【问题描述】:
嗨,我是 Android 和 Java OOP 的初学者。我目前正在构建 Android 问答游戏。应用程序应显示 6 个问题和 4 个答案,如果玩家的答案是正确的,那么玩家将获得 5 分并移动到另一个问题,如果玩家回答不正确的问题,玩家将不得分但会移动到不同的问题,反馈给玩家应该也可以提供(我打算用 Toast 来做这个)。最后,玩家将进入最终屏幕,查看总分和正确答案。现在,我不知道如何使用 ArrayList 显示 6 个问题和 4 个答案?问题和答案也应该存储在文件中(我不打算使用 SQLdbhelper)
我以前是测验/琐事游戏的新手。这是我到目前为止所做的:
public class play extends Activity implements OnClickListener {
private int totalcorrectanswers;
private int correctanswers;
private Random random;
private TextView questiontextview;
private int score;
private int finalscore;
Button answer1,answer2,answer3,answer4;
Button AnswerButtons [] = {answer1,answer2,answer3,answer4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
View AnswerButton1 = findViewById(R.id.answerbutton1);
AnswerButton1.setOnClickListener(this);
View AnswerButton2 = findViewById(R.id.answerbutton2);
AnswerButton2.setOnClickListener(this);
View AnswerButton3 = findViewById(R.id.answerbutton3);
AnswerButton3.setOnClickListener(this);
View AnswerButton4 = findViewById(R.id.answerbutton4);
AnswerButton4.setOnClickListener(this);
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity = "center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/scorenumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/questions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:gravity="center" >
<Button
android:id="@+id/answerbutton1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/answerbutton2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/answerbutton3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/answerbutton4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
【问题讨论】:
-
您可以使用
SharedPreferences将答案持续到一个周期结束,以查看有多少答案是正确的。现在问题如果你暂时没有想到bd,你可以使用strings文件来组织问题! -
@André.C.S 不,SharedPreferences 用于设置,而不是存储程序数据。
-
你应该使用
SQLdbhelper,这就是它的用途。 -
其实
SharedPreferences现在有多种用途,非常习惯于持久化简单的数据结构,互联网上使用SharedPreferences的例子不胜枚举,除了存储配置值还有其他用途看到它Quiz App Tutorial (Shared Preferences Tutorial) -
那么,如果不在 SQLite 中,您将问题和答案存储在哪里?文件?我猜你可以为此使用 XML 结构。