【问题标题】:Making a Continue button work using SharedPreferences使用 SharedPreferences 使 Continue 按钮工作
【发布时间】:2016-08-04 19:28:35
【问题描述】:

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

主要活动有两个按钮:一个新游戏按钮(可以正常工作)和一个继续按钮 - 一个让我感到困扰的按钮。显然,我想要的是,当按下时,它会将您带到您遇到的最后一个问题。我知道必须使用 SharedPreferences 才能完成这项工作,我的问题是我想不出一段有效的代码(我看过和阅读的每个教程都是关于保持名字 - 高分等)。

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

主要活动 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(下一个相同)

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

提前致谢,这几天我一直在寻找解决方案。

【问题讨论】:

    标签: java android sharedpreferences android-button


    【解决方案1】:

    这样做的一种方法是在 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() 方法中,您可以进行一系列检查以查看用户已到达多远并将其发送到相应的问题活动。

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

    编辑:您可以随时调用第一部分代码来保存某个问题的数据。例如,如果您的用户在 Question05 的活动类中回答了第 5 个问题,您将在确认用户回答正确后立即运行第一段代码。比如:

    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() 方法中进行所有检查之前,或者在您想知道他们回答了哪些问题以及他们没有回答哪些问题之前,在 MainActivity 中调用它t.

    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)部分,但我无法理解前两个应该放在哪里。通过“文件”(我在其中编写 SharedPreferences),您的意思是我应该创建一个新的 Java 选项卡,并将它们放在那里?对此我真的很抱歉,但我只是掌握了它:)
    • 没问题。对于第二个问题,SharedPreferences 将数据保存到应用存储中的 XML 文件中。因此,当您在对 getSharedPreferences() 的调用中提供文件名时,它会打开该文件进行读写(如果该文件不存在,它也会创建该文件)。除了确保打开保存数据的同一文件之外,您实际上不必过多担心此参数。至于你的第一个问题,我已经添加了一个更新来回答它。
    • 现在一切都开始变得有意义了——多亏了你,我开始理解你的代码背后的逻辑。最后一件事......虽然一切看起来都很好,但我的 (this,Question01.class);部分(主要活动的意图),并出现一条消息,显示“无法解析构造函数意图”。我通过将“this”替换为“MainActivity.this”找到了解决方案,但现在 90% 的时间我按继续,它只带我到 Question01。一旦它让我回答了最后一个问题——另一个问题什么也没做。我知道这太过分了,真的不能感谢你。
    • 你能告诉我你在哪里创建 Intent 吗?
    • 刚刚查看了我的代码 - 这是一个拼写错误,导致按钮发疯。再次衷心感谢您的耐心和帮助 :) 祝您的职业伙伴好运,已经检查了您的应用程序!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多