【问题标题】:Multiscreen app saving data between activities多屏应用程序在活动之间保存数据
【发布时间】:2017-02-28 18:18:06
【问题描述】:

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

  1. 我把所有的逻辑代码放在哪里?我的意思是单击提交答案按钮时调用的方法以及计算每个问题结果的 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. 我的第二个问题是关于如何保存每个问题的答案,以便用户可以在活动之间切换并且答案不会丢失,并且可以传递结果以计算最终分数?

我将非常感谢您的帮助,因为我现在真的被困在这个问题上......

谢谢!

【问题讨论】:

    标签: android multiscreen


    【解决方案1】:

    Justyna..我认为 4 个问题不需要 4 个活动。您应该使用查看寻呼机。 请阅读https://developer.android.com/training/animation/screen-slide.html的文档

    【讨论】:

      【解决方案2】:

      1 答案。您将在单独的类(如助手类)中完成所有逻辑部分,并将该方法调用到您的活动中。

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

      1. 回答。如果是少量数据,您可以使用共享偏好。

      在 android 中使用 parcelable 在活动之间共享数据。

      【讨论】:

        【解决方案3】:

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

        您可以将一个会话的所有数据全局存储到应用程序上下文中。 并通过从第四个活动传递应用程序上下文中的所有数据来创建一个非活动类来计算测验结果。 稍后您可以将数据存储在 Sql-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;
            }
        
        }
        

        【讨论】:

        • 感谢您的回答。我需要检查一下,因为我是初学者,这对我来说是全新的概念。您如何看待使用intent.putExta(),这在这里有用吗?好像简单了一点。
        • 是的,您可以通过 Intent 将数据从一个 Activity 发送到另一个 Activity 并在 Bundle 中传递信息,然后传递到 Intent 但请确保您应该将 Activity 1 的所有信息添加到 Activity 2 然后 3 结束,依此类推。在第四个活动中,您可以管理您的测验详细信息并计算结果。
        猜你喜欢
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多