【问题标题】:Developing Android trivia quiz game开发Android琐事问答游戏
【发布时间】: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 结构。

标签: java android


【解决方案1】:

首先,您创建一个问答模型(在课堂下方显示)。

public class MyQuestion {

private String question;
private ArrayList<String> ans;

public MyQuestion(String question, ArrayList<String> ans) {
    this.question = question;
    this.ans = ans;
}

public String getQuestion() {
    return question;
}

public ArrayList<String> getAns() {
    return ans;
}

}

之后为您的问题和答案创建一个静态数组列表。第一次您的数组列表为空,然后添加您的问题和答案。下次您的数组列表包含问题和答案。

public class MyActivity extends FragmentActivity {

static ArrayList<MyQuestion> myQuestions = new ArrayList<MyQuestion>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (myQuestions.isEmpty()) {
        addQuestionAndAnswer();
    }

}

private void addQuestionAndAnswer() {
    // here add your question and answer
}

}

【讨论】:

  • 你确定那个静态列表吗?
  • @Zhuinden 我认为是的,因为此列表仅在应用程序第一次打开时创建一次。这个应用程序中的第二件事只有 6 个问题。如果有更多问题,那就不行了。
  • @Divyang 嘿,谢谢! :) 但我在 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 上遇到错误因为它必须实现超类型方法。
  • @user3829911 编辑了我的答案,请检查。如果有任何问题,请告诉我。
  • @Diyang 嘿谢谢 :) 抱歉忘了问,这是否意味着我必须使用 ListView?我刚刚编辑了我的问题。你现在可以看到我的 XML 文件了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
相关资源
最近更新 更多