多屏应用在活动之间保存数据

时间:2017-02-28 09:00:00

标签: android multiscreen

我正在构建一个多屏幕测验应用程序,其中每个问题都在一个单独的活动中(我只有4个问题)。我已经设置了所有意图,因此我的应用程序在屏幕之间顺利进行(用户可以单击下一个/上一个转到下一个/上一个问题)。 在包含第四个问题的最后一个屏幕上,我添加了#34;提交答案"按钮。用户单击按钮,应该显示一个带有测验结果的Toast消息。我有两个问题:

  1. 我在哪里放置所有逻辑代码?我的意思是单击Submit Answers按钮时调用的方法和if else语句,我计算每个问题的结果。我可以将所有逻辑放在问题4活动中,还是应该在活动之间拆分,还是应该创建一个单独的类,我只放置这个逻辑?请参阅下面我所指的逻辑:

    // This method is called when the Submit Answers button is clicked

    public void submitAnswers(View view) {
    
    //Getting the answer to question 1
    EditText answerQ1 = (EditText) findViewById(R.id.answer_robots);
    String answer = answerQ1.getText().toString();
    
    //Getting the answer to question 2 checkbox 1
    CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
    boolean isCheckBox1Q2 = checkBox1Q2.isChecked();
    
    //Getting the answer to question 2 checkbox 2
    CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
    boolean isCheckBox2Q2 = checkBox2Q2.isChecked();
    
    //Getting the answer to question 2 checkbox 3
    CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
    boolean isCheckBox3Q2 = checkBox3Q2.isChecked();
    
    //Getting the answer to question 3 checkbox 1
    CheckBox checkBox1Q3 = (CheckBox) findViewById(R.id.checkbox1Q3);
    boolean isCheckBox1Q3 = checkBox1Q3.isChecked();
    
    //Getting the answer to question 3 checkbox 2
    CheckBox checkBox2Q3 = (CheckBox) findViewById(R.id.checkbox2Q3);
    boolean isCheckBox2Q3 = checkBox2Q3.isChecked();
    
    //Getting the answer to question 3 checkbox 3
    CheckBox checkBox3Q3 = (CheckBox) findViewById(R.id.checkbox3Q3);
    boolean isCheckBox3Q3 = checkBox3Q3.isChecked();
    
    //Getting the answer to question 4 radio button 1
    RadioButton radioButton1Q4 = (RadioButton) findViewById(R.id.radiobutton1Q4);
    boolean isRadioButton1Q4 = radioButton1Q4.isChecked();
    
    //Calculate Question 1 result
    int resultQ1 = calculateResultQ1(answer);
    
    //Calculate Question 2 result
    int resultQ2 = calculateResultQ2(isCheckBox1Q2, isCheckBox2Q2, isCheckBox3Q2);
    
    //Calculate Question 3 result
    int resultQ3 = calculateResultQ3(isCheckBox1Q3, isCheckBox2Q3, isCheckBox3Q3);
    
    //Calculate Question 4 result
    int resultQ4 = calculateResultQ4(isRadioButton1Q4);
    
    //Calculate the quiz result
    int result = resultQ1 + resultQ2 + resultQ3 + resultQ4;
    
    //Display the quiz result in the Toast message
    Toast.makeText(this, "Congrats! Your score is " + result + ". Thank you for taking the quiz!", Toast.LENGTH_LONG).show();
    
    }
    
    /**
     * Check the answer to the open question 1
     *
     * @param userAnswer is the user's answer to the question 1
     * @return the score the user got for question 1
     */
    private int calculateResultQ1(String userAnswer) {
    int result = 0;
    String answer = "Robina";
    if (userAnswer.equals(answer)) {
        result = 1;
    }
    return result;
    }
    
    /**
     * Check which checkbox was selected in the question 2
     *
     * @param checkBox1 is whether or not the user checked the checkbox1
     * @param checkBox2 is whether or not the user checked the checkbox2
     * @param checkBox3 is whether or not the user checked the checkbox3
     * @return the score the user got for question 2
     */
    private int calculateResultQ2(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
    int result = 0;
    if (checkBox1 && checkBox2 && checkBox3) {
        result = 1;
    }
    return result;
    }
    
    /**
     * Check which checkbox was selected in the question 3
     *
     * @param checkBox1 is whether or not the user checked the checkbox1
     * @param checkBox2 is whether or not the user checked the checkbox2
     * @param checkBox3 is whether or not the user checked the checkbox3
     * @return the score the user got for question 3
     */
    private int calculateResultQ3(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
    int result = 0;
    if (checkBox1 && checkBox2) {
        result = 1;
    }
    
    if (checkBox3) {
        result = 0;
    }
    return result;
    }
    
    /**
     * Check which radio button was selected in the question 4
     *
     * @param radioButton1 is whether or not the user checked the radio       button 1
     * @return the score the user got for question 4
     */
    private int calculateResultQ4(boolean radioButton1) {
    int result = 0;
    if (radioButton1) {
        result = 1;
    }
    return result;
    }
    
    1. 我的第二个问题是如何保存每个问题的答案,以便用户可以在活动之间切换,答案不会丢失,结果也可以通过计算最终得分?
  2. 我非常感谢你的帮助,因为我现在真的被困在这......

    谢谢!

3 个答案:

答案 0 :(得分:0)

Justyna ..我不认为你需要4个问题的4个活动。你应该使用view pager。 请阅读https://developer.android.com/training/animation/screen-slide.html

上的文档

答案 1 :(得分:0)

1 Ans。您将在单独的类中执行所有逻辑部分,如助手类,并将该方法调用到您的活动中。

你也可以在你的活动中做逻辑部分

  1. 答。如果是少量数据,则可以使用共享首选项。
  2. 在android中使用parcelable来获取活动之间的共享数据。

答案 2 :(得分:0)

全局变量或应用程序上下文变量 -

您可以将一个会话的所有数据存储到全局的应用程序上下文中。 并创建一个非Activity类,通过传递来自Fourth Activity的Application Context中的所有数据来计算测验结果。 之后,您可以将数据存储在Sq-lite数据库中以备将来使用。

下面我提供一个链接来说明如何为一个会话实现数据存储的应用程序上下文。

Global Variable Or Application Context Variable - Android Example

GlobalClass.java

创建android.app.Application类的自定义类子类。您将使用此类作为应用程序环境(Conext)的全局类。

package com.androidexample.globalvariable;

import android.app.Application;

public class GlobalClass extends Application{

    private String name;
    private String email;


    public String getName() {

        return name;
    }

    public void setName(String aName) {

       name = aName;

    }

    public String getEmail() {

        return email;
    }

    public void setEmail(String aEmail) {

      email = aEmail;
    }

}