私は、対応するメッセージが反映されている(プレイヤーXまたはプレーヤーY)それが誰であるかを示すtictactoeアプリケーションを作成しています。これは何の問題もなく機能しました。次に、ユーザーがPlayer XとPlayer Yに特定の名前を付けることができるPreferencesとFragmentsを使用して機能を追加しました。メッセージはその名前を実装します。しかし、今ではいつも、私はアプリを開くたびにゲームの前のセッションを覚えています。前回の一時停止時と同じようにゲームをロードすると思います。エミュレータが開くたびに新しいゲームを読み込むにはどうすればよいですか?Androidスタジオでロードされたときに以前のセッションがロードされます
私はそれが単純なものだと確信しています。私はAndroidStudioを初めて使ったばかりで、アイデアはありません。ありがとうございました!
これは、これは私の断片コードである私の活動コード
package com.asebastian.tictactoe;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class TictactoeActivity extends Activity implements OnClickListener {
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private TextView txtmsg;
private int r = 0;
private String btn1txt = " ";
private String btn2txt = " ";
private String btn3txt = " ";
private String btn4txt = " ";
private String btn5txt = " ";
private String btn6txt = " ";
private String btn7txt = " ";
private String btn8txt = " ";
private String btn9txt = " ";
private String Xname = " ";
private String Yname = " ";
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tictactoe);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
btn3 = (Button)findViewById(R.id.btn3);
btn4 = (Button)findViewById(R.id.btn4);
btn5 = (Button)findViewById(R.id.btn5);
btn6 = (Button)findViewById(R.id.btn6);
btn7 = (Button)findViewById(R.id.btn7);
btn8 = (Button)findViewById(R.id.btn8);
btn9 = (Button)findViewById(R.id.btn9);
txtmsg = (TextView)findViewById(R.id.txtMessage);
txtmsg.setText("Player X's turn");
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.tictactoe,menu);
return true;
}
@Override
public void onPause(){
SharedPreferences.Editor editor = prefs.edit();
editor.putString("btn1txt",btn1.getText().toString());
editor.putString("btn2txt",btn2.getText().toString());
editor.putString("btn3txt",btn3.getText().toString());
editor.putString("btn4txt",btn4.getText().toString());
editor.putString("btn5txt",btn5.getText().toString());
editor.putString("btn6txt",btn6.getText().toString());
editor.putString("btn7txt",btn7.getText().toString());
editor.putString("btn8txt",btn8.getText().toString());
editor.putString("btn9txt",btn9.getText().toString());
editor.putInt("r",r);
editor.commit();
super.onPause();
}
@Override
public void onResume(){
super.onResume();
btn1.setText(prefs.getString("btn1txt",""));
btn2.setText(prefs.getString("btn2txt",""));
btn3.setText(prefs.getString("btn3txt",""));
btn4.setText(prefs.getString("btn4txt",""));
btn5.setText(prefs.getString("btn5txt",""));
btn6.setText(prefs.getString("btn6txt",""));
btn7.setText(prefs.getString("btn7txt",""));
btn8.setText(prefs.getString("btn8txt",""));
btn9.setText(prefs.getString("btn9txt",""));
Xname = prefs.getString("pref_edittextX","");
Yname = prefs.getString("pref_edittextY","");
txtmsg.setText(Xname + "'s turn");
r = prefs.getInt("r",0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.new_game){
btn1.setEnabled(true);
btn2.setEnabled(true);
btn3.setEnabled(true);
btn4.setEnabled(true);
btn5.setEnabled(true);
btn6.setEnabled(true);
btn7.setEnabled(true);
btn8.setEnabled(true);
btn9.setEnabled(true);
btn1.setText("");
btn2.setText("");
btn3.setText("");
btn4.setText("");
btn5.setText("");
btn6.setText("");
btn7.setText("");
btn8.setText("");
btn9.setText("");
r = 0;
txtmsg.setText("Player X's turn");
}
else if (item.getItemId() == R.id.action_settings) {
startActivity(new Intent(getApplicationContext(),SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn1) {
r++;
if ((r % 2) == 0) {
btn1.setText("O");
} else {
btn1.setText("X");
}
}
if (v.getId() == R.id.btn2) {
r++;
if ((r % 2) == 0) {
btn2.setText("O");
} else {
btn2.setText("X");
}
}
if (v.getId() == R.id.btn3) {
r++;
if ((r % 2) == 0) {
btn3.setText("O");
} else {
btn3.setText("X");
}
}
if (v.getId() == R.id.btn4) {
r++;
if ((r % 2) == 0) {
btn4.setText("O");
} else {
btn4.setText("X");
}
}
if (v.getId() == R.id.btn5) {
r++;
if ((r % 2) == 0) {
btn5.setText("O");
} else {
btn5.setText("X");
}
}
if (v.getId() == R.id.btn6) {
r++;
if ((r % 2) == 0) {
btn6.setText("O");
} else {
btn6.setText("X");
}
}
if (v.getId() == R.id.btn7) {
r++;
if ((r % 2) == 0) {
btn7.setText("O");
} else {
btn7.setText("X");
}
}
if (v.getId() == R.id.btn8) {
r++;
if ((r % 2) == 0) {
btn8.setText("O");
} else {
btn8.setText("X");
}
}
if (v.getId() == R.id.btn9) {
r++;
if ((r % 2) == 0) {
btn9.setText("O");
} else {
btn9.setText("X");
}
}
if (Xname.isEmpty() && Yname.isEmpty()) {
if ((r % 2) == 0) {
txtmsg.setText("Player X's turn");
} else {
txtmsg.setText("Player Y's turn");
}
} else {
if ((r % 2) == 0) {
txtmsg.setText(Xname + "'s turn");
} else {
txtmsg.setText(Yname + "'s turn");
}
}
btn1txt = btn1.getText().toString();
btn2txt = btn2.getText().toString();
btn3txt = btn3.getText().toString();
btn4txt = btn4.getText().toString();
btn5txt = btn5.getText().toString();
btn6txt = btn6.getText().toString();
btn7txt = btn7.getText().toString();
btn8txt = btn8.getText().toString();
btn9txt = btn9.getText().toString();
calcWinner();
}
public void calcWinner() {
if (btn1txt == "X" && btn2txt == "X" && btn3txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn1txt == "O" && btn2txt == "O" && btn3txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (btn4txt == "X" && btn5txt == "X" && btn6txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn4txt == "O" && btn5txt == "O" && btn6txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (btn7txt == "X" && btn8txt == "X" && btn8txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn7txt == "O" && btn8txt == "O" && btn9txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (btn1txt == "X" && btn4txt == "X" && btn7txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn1txt == "O" && btn4txt == "O" && btn7txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (btn2txt == "X" && btn5txt == "X" && btn8txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn2txt == "O" && btn5txt == "O" && btn8txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (btn3txt == "X" && btn6txt == "X" && btn9txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn3txt == "O" && btn6txt == "O" && btn9txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (btn1txt == "X" && btn5txt == "X" && btn9txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn1txt == "O" && btn5txt == "O" && btn9txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (btn3txt == "X" && btn5txt == "X" && btn7txt == "X") {
txtmsg.setText("Player X wins!");
disableButtons();
}
if (btn3txt == "O" && btn5txt == "O" && btn7txt == "O") {
txtmsg.setText("Player Y wins!");
disableButtons();
}
if (r == 9) {
txtmsg.setText("Tie Game!");
disableButtons();
}
}
public void disableButtons(){
btn1.setEnabled(false);
btn2.setEnabled(false);
btn3.setEnabled(false);
btn4.setEnabled(false);
btn5.setEnabled(false);
btn6.setEnabled(false);
btn7.setEnabled(false);
btn8.setEnabled(false);
btn9.setEnabled(false);
}
}
です:
package com.asebastian.tictactoe;
import android.os.Bundle;
import android.preference.PreferenceFragment;
/**
* A simple {@link Fragment} subclass.
*/
public class SettingsFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
これは、フラグメントを追加するコードです:
package com.asebastian.tictactoe;
import android.app.Activity;
import android.os.Bundle;
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,new
SettingsFragment()).commit();
}
}
感謝を参照してください!それは大いに役立ちます。間違いなく使いやすく理解できるテクニックです。 – aseb1986
@ aseb1986ようこそ、あなたの質問に出くわすかもしれない他の人々のための答えとしてそれを受け入れることを検討してください。 –