1

我的目标是在用户导航回我的应用程序时显示模式对话框。此模式对话框要求他们提供他们定义的 6 位 PIN。我是 Java GUI 和 Android 开发的新手,所以我的问题是.....

我发现如果用户选择在显示输入对话框时最小化应用程序,如果他们返回到应用程序,则对话框会相互堆叠。让他们在输入 PIN 期间最小化并返回应用程序的次数尽可能多地输入他们的 PIN。

/**
 * 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 度,反之,我的对话框就会消失。有人可以指出发生这种强制重绘时调用的方法链,以便我可以重绘对话框吗?

4

2 回答 2

2

我相信您正在寻找onResume();+onPause();而不是onRestart();创建对话框。您应该使用onRestart();为要显示的 PIN 对话框设置一个标志,但不用于创建对话框。

我建议您将其移至onCreate();方法,使其成为您正在处理的活动的属性,然后简单地在onResume();方法中显示它(因为每次活动活动时都会调用它,即使是第一次)。

此外,当方向改变时,您需要列出您想要监听的方向改变事件(通过android:configChanges="orientation"在活动清单中),并且该onConfigurationChanged();方法将在活动活动上调用相应的数据。然后,您可以将对话框重绘到新的方向。

于 2012-06-20T12:43:50.727 回答
1

我通过将对话框存储在活动的属性中来解决相同的问题(引脚输入或其他),每当我显示对话框时,我都会使用以下内容:

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;

    }

当活动离开堆栈顶部(导航或退出)时,我也会处理当前对话框。

@Override
    public void onPause() {
        if (this.currentDialog != null) {
            this.currentDialog.dismiss();
        }

        super.onPause();
    }

onPause 方法允许您在暂停活动时关闭任何已经打开的对话框,这样您现有的 onRestart 代码只会显示一个新对话框。

于 2012-06-20T12:34:56.047 回答