2016-08-04 4 views
2

私はJavaとAndroidスタジオに関する限り、かなり初心者です。だから、私は単純な謎のゲームを構築しています。各アクティビティは質問とテキストフィールドで構成されています。正しい答えは次のものに移動します。SharedPreferencesを使用して続行ボタンを作成する

メインアクティビティには2つのボタンがあります:新しいゲーム1つと、私を困らせるもの明らかに、私が欲しいのは、押されると、あなたが最後にやった質問にあなたが連れて行くことです。私は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); 
    } 
} 

質問1つの活性の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

これを実行する1つの方法は、ユーザーが各質問に正しく答えたかどうかをSharedPreferencesに保存することです。あなたのMainActivityで、正しく回答した人の数に応じて適切な質問にそれらを送ってください。例:ここでは

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

、FILENAMEは、ファイルの名前は、あなたが最初の質問のために下のブールを救うタグだろうと「Question01」にあなたのSharedPreferencesを書くことです。

SharedPreferences prefs = getSharedPreferences("FILENAME", MODE_PRIVATE); 
boolean question01Answered = prefs.getBoolean("Question01", false); 

次に、質問1に答えたかどうかを確認するために上記を呼び出します。続行ボタンの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... 
} 

EDIT:コードの最初のセクションでは、特定の質問のためにデータを保存したい、いつでも呼び出すことができます。たとえば、ユーザーがQuestion05のアクティビティクラス内で5番目の質問に答えた場合、ユーザーが正しく回答したことを確認した直後に、最初のコードを実行します。あなたは、彼らがきた疑問を知りたい時はいつでも他の右あなたの継続ボタンのonClick()メソッド内のすべてのチェックを行う前にMainActivityでそれを呼び出す、

コードの2番目の部分については
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 
     } 
    } 

} 

、または:ような何か彼らは答えなかった。

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

はお時間をいただき、ありがとうございます:)私はあなたの作品を追加する必要があり、私は完全にどこに自分のコードに理解していないために謝罪する必要が...私は3分の1(のonClick)の部分を得たが、私は理解できません最初の2つは置くつもりです。 「ファイル」(SharedPreferencesを書き込む場所)では、新しいJavaタブを作成してそこに配置する必要がありますか?私は本当にこれについて残念ですが、私はちょうどそれの把握を得ています:) – Animated24

+0

問題はありません。あなたの2番目の質問のために、SharedPreferencesはあなたのアプリのストレージのXMLファイルにデータを保存します。したがって、getSharedPreferences()の呼び出しでファイル名を指定すると、読み書き用のファイルが開きます(ファイルが存在しない場合はファイルも作成されます)。データを保存した同じファイルを開いていることを確認するだけでなく、実際にこのパラメータを心配する必要はありません。 あなたの最初の質問として、私はそれに答えるためのアップデートを追加しました。 – Waves

+0

すべてが意味をなさないようになりました。あなたのおかげで、コードの背後にあるロジックを理解し始めました。一つの最後のこと...すべてがうまくいくように見えますが、私は私の中で赤い下線を引いています(これはQuestion01.classです)。パーツ(主なアクティビティのインテント)が表示され、「コンストラクタインテントを解決できません」というメッセージが表示されます。私は "this"を "MainActivity.this"に置き換えて解決策を見つけましたが、今は90%の時間を続けていますが、それはQuestion01にかかります。一度それが私に最後の質問に答えた - 別のものは何もしなかった。私はそれがあまりにも多いことを知っている、本当にあなたにこれについて十分にあなたに感謝できない。 – Animated24

関連する問題