0
在我的android猜謎遊戲應用中,我設法讓它在用戶在一個活動上輸入他們的名字時顯示在遊戲本身上。然而,我的問題發生在用戶點擊「是」時再次出現。遊戲運行正常,但名字消失。我知道這是因爲活動重新開始,但沒有被告知要傳遞名稱輸入,因爲這是通過onOptionsItemsSelected方法完成的,並且單擊yes是通過OnClickListener方法,但我不明白我應該如何「合併」這兩個,如果這是一個恰當的話語轉向。從用戶那裏獲取名稱輸入時,如何讓它在重新啓動時保持活動狀態?
我的類是下面:
public class GameCentral extends AppCompatActivity {
int backButtonCount = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_central_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.commonmenus, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
**public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.guessingGame) {
// displays name entered on the game itself
startActivity(new Intent(this, Task3Activity.class));
switch (item.getItemId()) {
case R.id.guessingGame:
Intent intent = new Intent(GameCentral.this, Task3Activity.class);
EditText playerName = (EditText) findViewById(R.id.playerNameInput);
intent.putExtra("PlayerName", playerName.getText().toString());
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}**
} else if (id == R.id.placeOne) {
Toast.makeText(this, "Game Placeholder One option is Clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.placeTwo) {
Toast.makeText(this, "Game Placeholder Two option is Clicked", Toast.LENGTH_SHORT).show();
} else if (id == R.id.placeThree) {
Toast.makeText(this, "Game Placeholder Three option is Clicked", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
public void onBackPressed() {
// when the back button is pressed on the mobile, two presses takes you back out of the app completely
if (backButtonCount >= 1) {
Intent backIntent = new Intent(Intent.ACTION_MAIN);
backIntent.addCategory(Intent.CATEGORY_HOME);
backIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(backIntent);
} else {
Toast.makeText(this, "Press the back button again to close the application", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
}
而且....
public class Task3Activity extends AppCompatActivity {
public Button rtnBtn;
// button to return to Game Central at any point when clicked
public void init() {
rtnBtn = (Button)findViewById(R.id.returnBtn);
rtnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent homeScreen = new Intent(Task3Activity.this, GameCentral.class);
startActivity(homeScreen);
Toast toast2 = Toast.makeText(getApplicationContext(), "Thank you for playing. Come back soon", Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.CENTER_VERTICAL, 0, 350);
toast2.show();
}
});
}
String value;
int attempts = 0;
final int maxAttempts = 1;
Random randGen = new Random();
int ranNum;
private SoundPlayer sound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task3_layout);
init();
final String playerName = getIntent().getStringExtra("PlayerName");
if (playerName != null) {
TextView dataRcvd = (TextView) findViewById(R.id.playerNameEntered);
dataRcvd.setText(playerName);
}
sound = new SoundPlayer(this);
final TextView textResponse = (TextView) findViewById(R.id.txtResponse);
final TextView guessText = (TextView) findViewById(R.id.txtAnswer);
final EditText userGuess = (EditText) findViewById(R.id.etNumber);
final Button pressMe = (Button) findViewById(R.id.btnGuess);
final Button rtnButton = (Button) findViewById(R.id.returnBtn);
int min = 1;
int max = 19;
randGen = new Random();
// Generate number once
ranNum = randGen.nextInt(max - min + 1) + min;
// if the user enters '0' or a number higher than '19' this toast message is displayed
final Toast toast = makeText(getApplicationContext(), "Please guess between 0 and 20", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 350);
// When the button is clicked, it shows the text assigned to the txtResponse TextView box
pressMe.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean correct = false;
final AlertDialog.Builder alert = new AlertDialog.Builder(Task3Activity.this);
alert.setTitle("Unlucky");
alert.setCancelable(false);
alert.setMessage("You have guessed incorrectly three times. " +
"The answer was " + ranNum + ". " + "Would you like to play again?")
//.setCancelable(true)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//dialog.dismiss();
Intent i = new Intent(Task3Activity.this, Task3Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//TextView dataRcvd = (TextView) findViewById(R.id.playerNameEntered);
//dataRcvd.setText(playerName);
startActivity(i);
}
});
alert
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
Intent toy = new Intent(Task3Activity.this, GameCentral.class);
Toast toast2 = Toast.makeText(getApplicationContext(), "Thank you for playing. Come back soon", Toast.LENGTH_SHORT);
toast2.setGravity(Gravity.CENTER_VERTICAL, 0, 350);
toast2.show();
startActivity(toy);
}
;
});
final AlertDialog.Builder alert2 = new AlertDialog.Builder(Task3Activity.this);
alert2.setTitle("You Did It!");
alert2.setCancelable(false);
alert2.setMessage("The answer was " + ranNum + ". " + "Would you like to play again?")
// resets the game
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//dialog.dismiss();
Intent i = new Intent(Task3Activity.this, Task3Activity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
});
alert2
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
// takes the user back to the Game Central app
dialog.dismiss();
Intent toy = new Intent(Task3Activity.this, GameCentral.class);
startActivity(toy);
}
;
});
int userNumber = Integer.parseInt(userGuess.getText().toString());
if ((userNumber < 1) || (userNumber > 19)) {
toast.show();
} else if (userNumber < ranNum) {
guessText.setText("Your answer is too low. Guess again!");
guessText.setBackgroundColor(Color.YELLOW);
//sound.playBuzzerSound();
} else if (userNumber > ranNum) {
guessText.setText("Your answer is too high. Guess again!");
guessText.setBackgroundColor(Color.RED);
//sound.playBuzzerSound();
} else if (userNumber == ranNum) {
ranNum = randGen.nextInt(20);
//guessText.setText("You did it!");
//guessText.setBackgroundColor(Color.WHITE);
correct = true;
alert2.show();
sound.playApplauseSound();
}
if (attempts++ > maxAttempts && !correct) {
alert.show();
sound.playBooSound();
} else if (correct) {
} else {
String randText = "";
randText = Integer.toString(ranNum);
textResponse.setText("");
userGuess.setText("");
}
}
}
);
}
}
與往常一樣,任何提示,幫助或方向不勝感激。
謝謝
感謝。爲了澄清,我是否應該將第一部分放在GameCentral類中,並將其他類中的「獲取」部分?或相反亦然?或者同時在一個班級?在這方面還是一個新手。謝謝 –
當玩家第一次將他/她的名字輸入到設備中時,隨後在需要其他名稱時,可以使用第二部分來檢索第一部分中存儲的內容。你幾乎可以把第二部分放在你想要的任何地方,真的。 –
謝謝。我放棄了這一點。首先,第二部分,它不會讓我再次使用'sharedPreferences'。由於顯而易見的原因,它只在最後添加1。所以,把這條線留下來,並把第二部分作爲'playerName = sharedPreferences.getString(「Name1」,「Default name」);''只是當我選擇開始一個新遊戲時,在屏幕上用'默認名稱'替換我輸入的名稱 –