AlertDialog.Buildertypes的getWindow()方法未定义

来自Android的理念:从对话模糊和调暗背景窗口 。 我无法让我的对话框中的内容模糊。 当调用eula.getWindow()我收到此错误:

AlertDialog.Buildertypes的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); } 

getWindow()是对话框类的一种方法,而不是对话框构build器的方法。 你的代码应该看起来像这样:

 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常量, 不再支持窗口后面的模糊。 所以你的代码将来可能会崩溃。

eula是Builder,而不是对话本身。 尝试:

 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);