对于类型AlertDialog.Builder,方法getWindow()未定义

时间:2011-12-16 16:59:28

标签: java android alertdialog blur android-alertdialog

来自Android: Blurring and dimming background windows from dialog的想法。我无法将对话框下的内容弄糊涂。当调用eula.getWindow()时,我收到此错误:

  

对于类型AlertDialog.Builder

,方法getWindow()未定义

eula与主要活动的代码一起显示:

    EulaHelper.showEula(false, this);

非常感谢任何帮助。

    public static void showEula(final boolean accepted, final FragmentActivity activity) {
    AlertDialog.Builder eula = new AlertDialog.Builder(activity)
            .setTitle(R.string.eula_title)
            .setIcon(android.R.drawable.ic_dialog_info)
                .setMessage(activity.getString(R.raw.eula))
            .setCancelable(accepted);

    if (accepted) {
        // If they've accepted the EULA allow, show an OK to dismiss.
        eula.setPositiveButton(android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
    } else {
        // If they haven't accepted the EULA allow, show accept/decline buttons and exit on
        // decline.
        eula
                .setPositiveButton(R.string.accept,
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                setAcceptedEula(activity);
                                dialog.dismiss();
                            }
                        })
                .setNegativeButton(R.string.decline,
                        new android.content.DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                                activity.finish();
                            }
                        });
    }
    eula.show();
    WindowManager.LayoutParams lp = eula.getWindow().getAttributes();
    lp.dimAmount = 0.0F;
    eula.getWindow().setAttributes(lp);
    eula.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

}

2 个答案:

答案 0 :(得分:15)

getWindow()是对话框类的方法,而不是对话框构建器的方法。你的代码应该是这样的:

AlertDialog dlg = eula.show();
WindowManager.LayoutParams lp = dlg.getWindow().getAttributes();
lp.dimAmount = 0.0F;
dlg.getWindow().setAttributes(lp);
dlg.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

请注意,现在不推荐使用FLAG_BLUR_BEHIND常量,但窗口后面的模糊是no longer supported。因此,您的代码将来可能会中断。

答案 1 :(得分:5)

eula是构建器,而不是对话框本身。尝试:

final AlertDialog eulaDialog = eula.create();
eulaDialog.show();
WindowManager.LayoutParams lp = eulaDialog.getWindow().getAttributes();
lp.dimAmount = 0.0F;
eulaDialog.getWindow().setAttributes(lp);
eulaDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);