就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();
}
};
在此先感謝,我一直在尋找一個解決方案連續幾天。
感謝您的時間:)雖然我必須道歉,因爲我還沒有完全理解其中的我的代碼,我要補充你的作品......我得到的三分之一(的onClick)的一部分,但我不明白前兩個是要放在哪裏。通過「文件」(我寫我的SharedPreferences)你的意思是我應該創建一個新的java選項卡,並把它們放在那裏?我真的很抱歉,但我只是掌握了它:) – Animated24
沒問題。對於第二個問題,SharedPreferences會將數據保存到應用程序存儲中的XML文件中。因此,當您在調用getSharedPreferences()時提供文件名時,會打開該文件進行讀寫(如果文件不存在,也會創建該文件)。除了確保打開保存數據的同一文件之外,您實際上不必擔心此參數太多。 至於你的第一個問題,我已經添加了一個更新來回答它。 – Waves
現在一切都開始有意義 - 感謝你我開始理解你的代碼背後的邏輯。最後一件事......儘管一切看起來都很好,但我在我的(This,Question01.class)中得到了一個紅色的下劃線。部分(主要活動的意圖),並出現一條消息,提示「無法解析構造函數意圖」。我通過用「MainActivity.this」替換「this」找到了解決方案,但現在我按下了90%的時間繼續,它只是將我帶到Question01。一旦它讓我回答最後一個問題 - 另一個人什麼也沒做。我知道這太多了,真的不能爲此感謝你。 – Animated24