2016-08-04 63 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

相關問題