开发Android琐事测验游戏

时间:2014-07-20 14:14:49

标签: java android

嗨,我是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>

1 个答案:

答案 0 :(得分:0)

首先为问答创建一个模型(在下面的课程中显示)。

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;
}

}

之后为您的问题和答案创建一个静态arraylist。第一次你的arraylist是空的然后添加你的问题和答案。下一次你的arraylist是问题和答案。

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
}

}