0

我从 Activity 的 onPostResume 方法创建并显示一个警报对话框。该对话框未显示,但我不明白为什么。

我显示对话框的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("message");
builder.setPositiveButton("a", aListener);
builder.setPositiveButton("b", bListener);
builder.setCancelable(false);

AlertDialog dlg = builder.create();
dlg.show();
4

1 回答 1

1

尝试使用:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("message");
builder.setPositiveButton("a", aListener);
builder.setPositiveButton("b", bListener);
builder.setCancelable(false);
builder.show();

注意:没有理由创建另一个AlertDialog实例。


或者另一种正确的方法,您可以创建返回 new 的方法AlertDialog

protected static final int CREATE_INFORMATION_DIALOG = 1320;

private Dialog createDialog(int type) {
        AlertDialog dialog = null;
        switch (type) {
            case CREATE_INFORMATION_DIALOG:
                dialog = new AlertDialog.Builder(this)
                    .setTitle("Information")
                    .setMessage("Download was finished successfully.")
                    .setPositiveButton("Close", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dlg, int whichButton) {

                        }
                    })
                    .create();
                break;
        }
        return dialog;
    }

然后就这样称呼它

createDialog(CREATE_INFORMATION_DIALOG).show();
于 2012-07-22T13:33:15.343 回答