就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();
}
};
在此先感谢,我已经为此寻找解决方案了好几天。
答案 0 :(得分:0)
这样做的一种方法是保存用户是否在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);
然后你打电话给上面检查问题01是否已被回答。在您的继续按钮的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中调用它。吨。
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...
}