Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SharedReferences使“继续”按钮工作_Java_Android_Sharedpreferences_Android Button - Fatal编程技术网

Java 使用SharedReferences使“继续”按钮工作

Java 使用SharedReferences使“继续”按钮工作,java,android,sharedpreferences,android-button,Java,Android,Sharedpreferences,Android Button,就java和android studio而言,我是一个初学者。所以,我正在制作一个简单的谜语游戏。每个活动由一个问题和一个文本字段组成,用于放置您的答案-正确的答案将引导您进入下一个答案,等等 主要活动有两个按钮:一个是“新游戏一”(运行良好)按钮,另一个是“继续”按钮,这是一个困扰我的按钮。显然,我想要的是,当你被问到最后一个问题时,它会带你去问。我知道必须使用SharedReferences才能完成这项工作,我的问题是我没有想到一段代码是有效的(我所看到和阅读的每一篇教程都是关于保留姓名-高

就java和android studio而言,我是一个初学者。所以,我正在制作一个简单的谜语游戏。每个活动由一个问题和一个文本字段组成,用于放置您的答案-正确的答案将引导您进入下一个答案,等等

主要活动有两个按钮:一个是“新游戏一”(运行良好)按钮,另一个是“继续”按钮,这是一个困扰我的按钮。显然,我想要的是,当你被问到最后一个问题时,它会带你去问。我知道必须使用SharedReferences才能完成这项工作,我的问题是我没有想到一段代码是有效的(我所看到和阅读的每一篇教程都是关于保留姓名-高分等)

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

主要活动java

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);
    }
}
问题一个活动java(下一个活动相同)


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

一种方法是保存用户是否在SharedReferences中正确回答了每个问题。然后在你的主要活动中,根据他们正确回答的数量,将他们发送到正确的问题。例如:

SharedPreferences.Editor editor = getSharedPreferences("FILENAME", MODE_PRIVATE).edit();
editor.putBoolean("Question01", true);
editor.apply();
在这里,FILENAME是要将SharedReferences写入的文件名,“Question01”是第一个问题中保存布尔值的标记

SharedPreferences prefs = getSharedPreferences("FILENAME", MODE_PRIVATE);
boolean question01Answered = prefs.getBoolean("Question01", false);
然后你会打电话给上面的人,检查问题01是否得到了回答。在continue按钮的onClick()方法中,您可以执行一系列检查,查看用户已达到的程度,并将其发送到相应的提问活动

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类中,您将在确认用户回答正确后立即运行第一段代码。比如:

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

}
至于第二段代码,在继续按钮的onClick()方法中执行所有检查之前,或者在任何时候,您想知道他们回答了哪些问题,哪些问题没有回答时,在main活动中调用它

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

谢谢你的时间:)虽然我不得不为我还没有完全理解我应该在我的代码中添加你的片段而道歉。。。我得到了第三部分(onClick),但我不明白前两部分应该放在哪里。“文件”(我写共享引用的地方)你的意思是我应该创建一个新的java选项卡,并把它们放在那里?对此我真的很抱歉,但我只是想知道:)没问题。对于第二个问题,SharedReferences将数据保存到应用程序存储器中的XML文件中。因此,当您在对getSharedReferences()的调用中提供文件名时,它会打开该文件进行读写(如果该文件不存在,它还会创建该文件)。除了确保打开的文件与保存数据的文件相同外,实际上不必太担心这个参数。关于你的第一个问题,我已经添加了一个更新来回答它。现在一切都开始有意义了——多亏了你,我开始理解你代码背后的逻辑。最后一件事是。。。虽然一切看起来都很好,但我的(这是问题01.class)中有一条红色下划线;部分(主要活动的意图),并显示一条消息,说明“无法解析构造函数意图”。我通过将“this”替换为“MainActivity.this”找到了解决方案,但现在我90%的时间都是按continue,这只会让我进入问题01。有一次,它让我想到了最后一个问题——另一个问题什么也没做。我知道这太多了,真的太感谢你了。你能告诉我你在哪里创建了这个意图吗?刚刚检查了我的代码-是一个打字错误让按钮发疯了。我再次衷心感谢你的耐心和帮助:)祝你的职业伙伴好运,已经检查过你的应用程序了!
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...
}