2016-08-04 61 views
2

就java和android studio而言,我是一个初学者。所以,我正在构建一个简单的谜题游戏。每个活动都包含一个问题和一个文本字段来放置您的答案 - 正确的答案让你到下一个等。使用SharedPreferences制作继续按钮

主要活动有两个按钮:一个新的游戏一(工作正常)和一个继续 - 那个让我烦恼的人。很明显,我想要的是,当你按下时,它会把你带到你到达的最后一个问题。我知道必须使用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(); 
    } 
}; 

在此先感谢,我一直在寻找一个解决方案连续几天。

回答

0

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

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

这里,文件名是文件的名称,以你的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的活动类中正确回答第五个问题,那么在确认用户回答正确之后,您将立即运行第一段代码。喜欢的东西:

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()方法,或者当其他你想知道自己哪些问题回答,哪些是他们没有的。

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

感谢您的时间:)虽然我必须道歉,因为我还没有完全理解其中的我的代码,我要补充你的作品......我得到的三分之一(的onClick)的一部分,但我不明白前两个是要放在哪里。通过“文件”(我写我的SharedPreferences)你的意思是我应该创建一个新的java选项卡,并把它们放在那里?我真的很抱歉,但我只是掌握了它:) – Animated24

+0

没问题。对于第二个问题,SharedPreferences会将数据保存到应用程序存储中的XML文件中。因此,当您在调用getSharedPreferences()时提供文件名时,会打开该文件进行读写(如果文件不存在,也会创建该文件)。除了确保打开保存数据的同一文件之外,您实际上不必担心此参数太多。 至于你的第一个问题,我已经添加了一个更新来回答它。 – Waves

+0

现在一切都开始有意义 - 感谢你我开始理解你的代码背后的逻辑。最后一件事......尽管一切看起来都很好,但我在我的(This,Question01.class)中得到了一个红色的下划线。部分(主要活动的意图),并出现一条消息,提示“无法解析构造函数意图”。我通过用“MainActivity.this”替换“this”找到了解决方案,但现在我按下了90%的时间继续,它只是将我带到Question01。一旦它让我回答最后一个问题 - 另一个人什么也没做。我知道这太多了,真的不能为此感谢你。 – Animated24

相关问题