Java Android模态对话框堆叠
我的目标是在用户导航回我的应用程序时显示一个模式对话框。此模式对话框要求他们提供他们定义的6位PIN。我不熟悉Java GUI和Android开发,所以我的问题是 我发现,如果用户选择在显示enterpin对话框时最小化应用程序,如果他们返回应用程序,则对话框会堆叠在彼此的顶部。让他们输入PIN的次数尽可能减少,并在PIN输入期间返回应用程序Java Android模态对话框堆叠,java,android,xml,user-interface,Java,Android,Xml,User Interface,我的目标是在用户导航回我的应用程序时显示一个模式对话框。此模式对话框要求他们提供他们定义的6位PIN。我不熟悉Java GUI和Android开发,所以我的问题是 我发现,如果用户选择在显示enterpin对话框时最小化应用程序,如果他们返回应用程序,则对话框会堆叠在彼此的顶部。让他们输入PIN的次数尽可能减少,并在PIN输入期间返回应用程序 /** * On restart called when the app is being restarted on the screen */ @O
/**
* On restart called when the app is being restarted on the screen
*/
@Override
public void onRestart() {
super.onRestart();
// must prompt with modal dialog for pin
final Dialog dialog = new Dialog(this);
dialog.setCancelable(false);
// check pin
dialog.setCanceledOnTouchOutside(false);
// set view to enterpin XML screen
dialog.setContentView(R.layout.enterpin);
// show dialog
if (!dialog.isShowing()) {
dialog.show();
}
// listen for button being clicked
Button button = (Button) dialog.findViewById(R.id.pinlogin);
button.setOnClickListener(new OnClickListener() {// anonymous inner
// class
// implementation
@Override
public void onClick(View v) {
EditText editText = (EditText) dialog
.findViewById(R.id.enterpintext);
try {
int enteredPin = Integer.parseInt(editText.getText()
.toString());
SharedPreferences sharedP = Prefs.get(WebViewActivity.this);
int temp = sharedP.getInt("pin", 0);
if (enteredPin == temp) {
pinCheck = true;
dialog.dismiss();
} else {
pinCheck = false;
dialog.setTitle("Incorrect Pin");
}
} catch (NumberFormatException e) {
Dialog dialog2 = new Dialog(WebViewActivity.this);
dialog2.setTitle("Enter Numbers Only");
dialog2.setCanceledOnTouchOutside(true);
dialog2.show();
}
}
});
}
有没有什么地方我可以移动我的对话框初始化而不觉得这是糟糕的编程实践?我理解为什么我尝试的dialog.isShowing()方法不起作用,因为我的dialog实例只在该方法的生命周期内存在
我还注意到,如果你把手机从垂直方向转到水平方向90度,反过来,我的对话就会消失。有人能指出当强制重画发生时调用的方法链,这样我就可以重画对话框了吗?我通过在活动的属性中存储by dialog来解决同样的问题(pin输入或其他),每当我显示对话框时,我都会使用以下内容:
protected android.app.Dialog onCreateDialog(int id) {
if (this.currentDialog != null) {
this.currentDialog.dismiss();
}
this.currentDialog = null;
switch (id) {
case DIALOG_SPINNER:
ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage(getString(R.string.dialog_Spinner_UpdatingAccount));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
this.currentDialog = progressDialog;
break;
}
return this.currentDialog;
}
当活动离开堆栈顶部(导航或退出)时,我也会处理当前对话框
onPause方法允许您在暂停活动时关闭任何已打开的对话框,这样现有的onRestart代码就会显示一个新对话框。我相信您正在寻找
onResume()代码>+onPause()代码>而不是onRestart()代码>以创建对话框。您应该使用onRestart()
为要显示的管脚对话框设置标志,但不创建对话框
我建议您将其移动到onCreate()方法,使其成为您正在处理的活动的属性,并简单地将其显示在onResume()中代码>方法(因为每次活动处于活动状态时,即使是第一次,都会调用它)
此外,当方向改变时,您需要列出您想要收听方向改变事件(通过活动清单中的android:configChanges=“orientation”
)和onConfigurationChanged()将使用相应的数据对活动活动调用代码>方法。然后,您可以将对话框重新绘制为新的方向。您的代码解决了我的问题:调用setCanceledOnTouchOutside。但是,我将活动子类化,而不是对话框子类化;在这种情况下,调用被设置为FinishOnTouchOut。它与android:theme=“@android:style/theme.Dialog”结合使用,使活动的行为类似于对话框。
@Override
public void onPause() {
if (this.currentDialog != null) {
this.currentDialog.dismiss();
}
super.onPause();
}