1

私の目標は、ユーザーがアプリに戻ったときにモーダル ダイアログを表示することです。このモーダル ダイアログは、ユーザーが定義した 6 桁の PIN を要求します。私は Java GUI と Android の開発が初めてなので、私の問題は.....

Enterpin ダイアログが表示されているときにユーザーがアプリを最小化することを選択した場合、ユーザーがアプリに戻ると、ダイアログが互いに重なり合っていることがわかりました。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インスタンスはメソッドの存続期間中しか存在しないため、私が試したdialog.isShowing()メソッドが機能しない理由を理解しています。

また、携帯電話を縦向きから横向きに 90 度回転させたり、その逆にしたりすると、ダイアログが消えてしまうことにも気付きました。ダイアログを再描画できるように、この強制再描画が発生したときに呼び出される一連のメソッドを誰かが指摘できますか?

4

2 に答える 2

2

onResume();+を探していて、ダイアログを作成してonPause();いないと思います。ダイアログを作成するためではなく、PIN ダイアログを表示するためのフラグを設定するためにonRestart();使用する必要があります。onRestart();

メソッドに移動し、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 に答える