Java 当从用户处获取名称输入时,如何使其在重新启动时保持在活动上?
在我的android猜谜游戏应用程序中,当用户在一个活动中输入自己的名字时,它会显示在游戏本身上。但是,当用户单击“是”进行另一次尝试时,就会出现我的问题。游戏运行良好,但他们的名字消失了。我知道这是因为活动正在重新开始,但没有被告知将名称输入带到另一个位置,因为这是通过onOptionsItemsSelected方法完成的,单击yes是通过OnClickListener方法完成的,但我不明白如果这是一个恰当的措辞,我应该如何“合并”这两者 我的课程如下:Java 当从用户处获取名称输入时,如何使其在重新启动时保持在活动上?,java,android,onclicklistener,user-input,Java,Android,Onclicklistener,User Input,在我的android猜谜游戏应用程序中,当用户在一个活动中输入自己的名字时,它会显示在游戏本身上。但是,当用户单击“是”进行另一次尝试时,就会出现我的问题。游戏运行良好,但他们的名字消失了。我知道这是因为活动正在重新开始,但没有被告知将名称输入带到另一个位置,因为这是通过onOptionsItemsSelected方法完成的,单击yes是通过OnClickListener方法完成的,但我不明白如果这是一个恰当的措辞,我应该如何“合并”这两者 我的课程如下: public class GameCe
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("");
}
}
}
);
}
}
公共类Task3活动扩展了AppCompative活动{
公共按钮rtnBtn;
//单击按钮可随时返回游戏中心
公共void init(){
rtnBtn=(按钮)findViewById(R.id.returnBtn);
rtnBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
意图主屏幕=新意图(Task3Activity.this、GameCentral.class);
startActivity(主屏幕);
Toast Toast 2=Toast.makeText(getApplicationContext(),“感谢您的播放,请尽快回来”,Toast.LENGTH\u SHORT);
toast2.设置重力(重心垂直,0350);
toast2.show();
}
});
}
字符串值;
int=0;
最终整数最大尝试数=1;
Random randGen=新的Random();
int ranNum;
私人音响播放器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.task3_布局);
init();
最终字符串playerName=getIntent().getStringExtra(“playerName”);
if(playerName!=null){
TextView dataRcvd=(TextView)findViewById(R.id.playerNameCenter);
dataRcvd.setText(playerName);
}
声音=新的声音播放器(此);
最终TextView textResponse=(TextView)findViewById(R.id.txtResponse);
最终文本视图猜测文本=(文本视图)findViewById(R.id.txtAnswer);
最终EditText userGuess=(EditText)findViewById(R.id.etNumber);
最终按钮pressMe=(按钮)findViewById(R.id.b按钮);
最终按钮rtnButton=(按钮)findViewById(R.id.returnBtn);
int min=1;
int max=19;
randGen=新随机数();
//生成一次数字
ranNum=randGen.nextInt(最大-最小+1)+min;
//如果用户输入“0”或高于“19”的数字,将显示此toast消息
final Toast Toast=makeText(getApplicationContext(),“请猜0到20之间”,Toast.LENGTH\u LONG);
toast.setGravity(重心垂直,0350);
//单击按钮时,将显示分配给txtResponse文本视图框的文本
pressMe.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
布尔正确=错误;
final AlertDialog.Builder alert=新建AlertDialog.Builder(Task3Activity.this);
警惕。设置标题(“不吉利”);
警报。可设置可取消(错误);
setMessage(“您猜错了三次。”+
答案是“+ranNum+”+“你想再玩一次吗?”)
//.setCancelable(真)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//dialog.dismise();
意图i=新意图(Task3Activity.this,Task3Activity.class);
i、 添加标志(意图、标志、活动、清除、顶部);
//TextView dataRcvd=(TextView)findViewById(R.id.playerNameCenter);
//dataRcvd.setText(playerName);
星触觉(i);
}
});
警觉的
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int i){
dialog.dismise();
意向玩具=新意向(Task3Activity.this,GameCentral.class);
Toast Toast 2=Toast.makeText(getApplicationContext(),“感谢您的播放,请尽快回来”,Toast.LENGTH\u SHORT);
toast2.设置重力(重心垂直,0350);
toast2.show();
星触觉(玩具);
}
;
});
final AlertDialog.Builder alert2=新建AlertDialog.Builder(Task3Activity.this);
alert2.setTitle(“你做到了!”);
警报2.可设置可取消(错误);
alert2.setMessage(“答案是“+ranNum+”+“您想再次播放吗?”)
//重置游戏
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//dialog.dismise();
意图i=新意图(Task3Activity.this,Task3Activity.class);
i、 添加标志(意图、标志、活动、清除、顶部);
星触觉(i);
}
});
警报2
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口对话框,int i){
//将用户带回游戏中心应用程序
dialog.dismise();
意向玩具=新意向(Task3Activity.this,GameCentral)。
SharedPreferences sharedPreferences = getSharedPreferences("NameList", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Name1", playerName);
editor.apply();
SharedPreferences sharedPreferences = getSharedPreferences("NameList", MODE_PRIVATE);
String playerName = sharedPreferences.getString("Name1", "Default name");