使用 SharedPreferences 使继续按钮工作

Making a Continue button work using SharedPreferences

就 java 和 android 工作室而言,我是一个初学者。所以,我正在构建一个简单的谜语游戏。每个 activity 都包含一个问题和一个用于放置答案的文本字段 - 正确的答案会让您进入下一个等等。

主 activity 有两个按钮:一个是新游戏按钮(效果很好),另一个是继续按钮 - 那个让我很困扰。显然,我想要的是,按下时,它会将您带到您遇到的最后一个问题。我知道必须使用 SharedPreferences 才能完成此操作,我的问题是我想不出一段有效的代码(我看过和读过的每个教程都是关于保留名称 - 高分等)。

这是我的代码:它可能需要一些改进,但我仍在学习。

主要Activityjava

public class MainMenu extends AppCompatActivity {

    private Button mStartInterrogationButton;
    private VideoView mLogoprwto;
    private Button mContinueButton; //This one is the one I'm asking about

    @Override
    public void onResume() {
        super.onResume();
        setContentView(R.layout.activity_main_menu);


        mLogoprwto = (VideoView) findViewById(R.id.logoprwto);
        mLogoprwto.setVideoPath("android.resource://my.path/"+R.raw.teloslogo);
        mLogoprwto.start();

        mLogoprwto.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {

                mLogoprwto.start();
            }
        });

        mStartInterrogationButton = (Button)findViewById(R.id.StartInterrogationButton);
        mStartInterrogationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startGame();

            }
        });

    }

private void startGame () {
    Intent intent = new Intent(this, Question01.class);
    startActivity(intent);
    finish();
}
    @Override
    public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}

问题一activityjava(后面的一样)

    public class FirstQuestion extends AppCompatActivity {

    private Button mSayAnswer;
    private EditText mAnswerbox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_question);

        mAnswerbox = (EditText)findViewById(R.id.Answerbox);
        mSayAnswer = (Button)findViewById(R.id.SayAnswer);

        mSayAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String answer = mAnswerbox.getText().toString().trim();
                if (answer.equalsIgnoreCase("nothing")){
                    Intent i = new Intent(getApplicationContext(), QuestionTwo.class);
                    startActivity(i);

                }else if (answer.equalsIgnoreCase("black")) {
                    Intent i = new Intent(getApplicationContext(), QuestionTwo.class);
                    startActivity(i);

                }else {

                }
            }
        });
    }
    @Override
    public void onBackPressed()
    {
        startActivity(new Intent(getApplicationContext(), MainMenu.class));
        finish();
    }
};

提前致谢,我已经为此寻找解决方案好几天了。

这样做的一种方法是在 SharedPreferences 中保存用户是否正确回答了每个问题。然后在您的 MainActivity 中,根据他们正确回答的数量将他们发送到正确的问题。例如:

SharedPreferences.Editor editor = getSharedPreferences("FILENAME", MODE_PRIVATE).edit();
editor.putBoolean("Question01", true);
editor.apply();

这里,FILENAME 是要将您的 SharedPreferences 写入的文件的名称,'Question01' 是您要在第一个问题下保存布尔值的标签。

SharedPreferences prefs = getSharedPreferences("FILENAME", MODE_PRIVATE);
boolean question01Answered = prefs.getBoolean("Question01", false);

然后你会调用上面的方法来检查 Question01 是否已经被回答。在继续按钮的 onClick() 方法中,您可以进行一系列检查以查看用户到达了多远,并将它们发送到适当的问题 Activity.

if (!(question01Answered)) {
    // question01Answered is false and has not been answered. Send them to the first question.
    Intent intent = new Intent(this, Question01.class);
    startActivity(intent);
    finish();
} else if (!(question02Ansered)) {
    // question02Answered is false and has not been answered. Send them to the second question.
    Intent intent = new Intent(this, Question02.class);
    startActivity(intent);
    finish();
} else if (!(question03Answered)) {
   // and so on...
} 

编辑: 您可以随时调用代码的第一部分,以保存特定问题的数据。例如,如果您的用户正确回答了第 5 个问题,那么在 Question05 的 activity class 中,您将在确认用户回答正确后立即 运行 第一段代码。类似于:

public class Question05 extends AppCompatActivity {

    ...

    private void checkIfQuestion05IsCorrect() {
        if (correct) {
            // They answered right, move to the next question.
            // But before we do that, let's save the fact that they answered right.

            SharedPreferences.Editor editor = getSharedPreferences("FILENAME", MODE_PRIVATE).edit();
            editor.putBoolean("Question05", true);
            editor.apply();
        } else {
            // they got it wrong
        }
    }

}

至于第二段代码,请在 MainActivity 中调用它,然后再在继续按钮的 onClick() 方法中进行所有检查,或者在其他任何您想知道他们有哪些问题的时候调用它回答了哪些还没有回答。

SharedPreferences prefs = getSharedPreferences("FILENAME", MODE_PRIVATE);
boolean question01Answered = prefs.getBoolean("Question01", false);
boolean question02Answered = prefs.getBoolean("Question02", false);
boolean question03Answered = prefs.getBoolean("Question03", false);
// and so on ...

if (!(question01Answered)) {
    // question01Answered is false and has not been answered. Send them to the first question.
    Intent intent = new Intent(this, Question01.class);
    startActivity(intent);
    finish();
} else if (!(question02Ansered)) {
    // question02Answered is false and has not been answered. Send them to the second question.
    Intent intent = new Intent(this, Question02.class);
    startActivity(intent);
    finish();
} else if (!(question03Answered)) {
   // and so on...
}