我有一個在桌面佈局中創建的tic tac toe遊戲應用,遊戲中有三排9個按鈕,一個顯示輪到誰的文本視圖和一個底部的新遊戲按鈕。 我想在代碼中添加共享偏好設置,這樣遊戲就不會在重新啓動的情況下繼續。我知道我需要一個onPause和onResume方法,但我不確定方法中會發生什麼。有人可以幫助實現共享偏好的方法嗎?下面是最讓遊戲代碼:如何將共享偏好添加到tic tac toe Android遊戲?
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe);
//get references to the widgets
square1 = (Button) findViewById(R.id.square1);
square2 = (Button) findViewById(R.id.square2);
square3 = (Button) findViewById(R.id.square3);
square4 = (Button) findViewById(R.id.square4);
square5 = (Button) findViewById(R.id.square5);
square6 = (Button) findViewById(R.id.square6);
square7 = (Button) findViewById(R.id.square7);
square8 = (Button) findViewById(R.id.square8);
square9 = (Button) findViewById(R.id.square9);
messageTextView = (TextView) findViewById(R.id.messageTextView);
square1.setOnClickListener(this);
square2.setOnClickListener(this);
square3.setOnClickListener(this);
square4.setOnClickListener(this);
square5.setOnClickListener(this);
square6.setOnClickListener(this);
square7.setOnClickListener(this);
square8.setOnClickListener(this);
square9.setOnClickListener(this);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void onPause(){
}
public void onResume() {
super.onResume();
}
@Override
public void onClick(View v) {
boolean flag;
String lbl = messageTextView.getText().toString();
Button btn = (Button) v;
if (btn.getText().toString().equals("")){
if (lbl.equals("Player X\'s turn")){
btn.setText("X");
messageTextView.setText("Player O\'s turn");
}
else{
btn.setText("0");
messageTextView.setText("Player X\'s turn");
}
}
flag = determineWinner();
if (!flag){
checkAgain();
}
}
public boolean determineWinner(){
if (square1.getText().toString().equals("X") &&
square2.getText().toString().equals("X")
&& square3.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square1.getText().toString().equals("X")
&& square4.getText().toString().equals("X")
&& square7.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square4.getText().toString().equals("X")
&& square5.getText().toString().equals("X")
&& square6.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square7.getText().toString().equals("X")
&& square8.getText().toString().equals("X")
&& square9.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square2.getText().toString().equals("X")
&& square5.getText().toString().equals("X")
&& square8.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square3.getText().toString().equals("X")
&& square6.getText().toString().equals("X")
&& square9.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square1.getText().toString().equals("X")
&& square5.getText().toString().equals("X")
&& square9.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square3.getText().toString().equals("X")
&& square5.getText().toString().equals("X")
&& square7.getText().toString().equals("X"))
{
message = "Player X wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square1.getText().toString().equals("0")
&& square2.getText().toString().equals("0")
&& square3.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square4.getText().toString().equals("0")
&& square5.getText().toString().equals("0")
&& square6.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square7.getText().toString().equals("0")
&& square8.getText().toString().equals("0")
&& square9.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square1.getText().toString().equals("0")
&& square4.getText().toString().equals("0")
&& square7.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square2.getText().toString().equals("0")
&& square5.getText().toString().equals("0")
&& square8.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square3.getText().toString().equals("0")
&& square6.getText().toString().equals("0")
&& square9.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square1.getText().toString().equals("0")
&& square5.getText().toString().equals("0")
&& square9.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
if (square3.getText().toString().equals("0")
&& square5.getText().toString().equals("0")
&& square7.getText().toString().equals("0"))
{
message = "Player O wins";
messageTextView.setText(message);
disableButtons();
return true;
}
return false;
}
'我想給代碼添加共享偏好,這樣遊戲就不會重新啓動,如果我開玩笑的方向。「 - 爲什麼你的遊戲會在方向改變時重新啓動? – cosmincalistru
我不會使用SharedPreferences,而是使用onPause/onResume機制。 –
我不知道爲什麼遊戲重新啓動時,我改變方向 – user2295419