【发布时间】:2011-02-03 16:45:57
【问题描述】:
我正在开发一个 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));
}
}
如果我在显示对话框时按主页键。当我再次启动应用程序并单击保存按钮时,再次显示对话框。
我已经调试了我的代码,它运行良好,但对话框仍然打开。
发生了什么事?
谢谢。
【问题讨论】:
标签: android modal-dialog android-2.2-froyo