gpt4 book ai didi

android - 按主页键并返回应用程序后显示对话框两次

转载 作者:搜寻专家 更新时间:2023-11-01 09:15:41 25 4
gpt4 key购买 nike

我正在开发 Android 2.2 应用。

我使用对话框询问用户的昵称。这是我的源代码:

private void showDialog() {
//set up dialog
final Dialog dialog = new Dialog(UserStatsActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.createuserrow);
dialog.setOnDismissListener(this);

//set up button
Button button = (Button) dialog.findViewById(R.id.saveUser);
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
TextView nameTextView = (TextView)dialog.findViewById(R.id.userName);
userNickName = nameTextView.getText().toString().trim();

if ((userNickName.length() > 0) &&
(userNickName.length() < 9)){
saveUser = true;
dialog.dismiss();
}
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}

public void onDismiss(DialogInterface arg0) {
try {
if (saveUser) {
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
User.saveUser(getApplicationContext(), userNickName);
Editor editor = prefs.edit();
editor.putString(USER_NAME, userNickName);
editor.putBoolean(FIRST_TIME_RUN, false);
editor.commit();
loadPreferences();
}
}
catch (Exception ex){
Log.e(Constants.APP_TAG, "UActivity: " + ex.getMessage());
showAlert(this.getString(R.string.errorClose));
}
}

如果我在显示对话框时按主页键。当我再次启动应用程序并单击保存按钮时,会再次显示对话框。

我已经调试了我的代码,它运行良好,但对话框仍然打开。

发生了什么事?

谢谢。

最佳答案

我会使用 onCreateDialog() 和 onPrepareDialog() 方法。通过这样做,您可以更好地控制何时填充和显示对话框。

onCreateDialog() 在对话框首次显示时触发。此方法是用于设置对话框的方法。

onPrepareDialog() 在每次显示之前触发。此方法是在后续每次更改选项的方法。

@Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
final Dialog dialog = new Dialog(UserStatsActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.createuserrow);
dialog.setOnDismissListener(this);

//set up button
Button button = (Button) dialog.findViewById(R.id.saveUser);
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
TextView nameTextView = (TextView)dialog.findViewById(R.id.userName);
userNickName = nameTextView.getText().toString().trim();

if ((userNickName.length() > 0) && (userNickName.length() < 9)){
saveUser = true;
dialog.dismiss();
}
}
});
//now that the dialog is set up, it's time to show it
return dialog;
}

这就是在您的情况下如何使用 onCreateDialog(int id) 方法。现在,请记住上面的内容,我没有考虑您可以创建的任何其他对话框,因此您应该为您的 Dialog 分配一个常量 Int 并从此方法中确定。

另请记住,此方法 在第一个对话框中被调用。您必须使用:

@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
super.onPrepareDialog(id, dialog, args);
if(id == DIALOG_DEFAULT) {
dialog.setTitle(mSettings.getString(getString(R.string.key_due_date), getString(R.string.unknown_task)));
((AlertDialog)dialog).setMessage(mSettings.getString(getString(R.string.key_task), getString(R.string.unknown_task)));
}
}

要为每个后续对话框重新填充,或将 removeDialog(int DIALOG_ID); 添加到您的 onClick。

关于android - 按主页键并返回应用程序后显示对话框两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4888950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com