2011-12-16 70 views
7

取自Android: Blurring and dimming background windows from dialog的想法。我無法讓我的對話框中的內容模糊。當調用eula.getWindow()我收到此錯誤:對於AlertDialog.Builder類型,未定義getWindow()方法

The method getWindow() is undefined for the type AlertDialog.Builder

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

} 

回答

11

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。所以你的代碼在將來可能會崩潰。

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

儘管兩個答案都是正確的, alextsc的答案更符合我的代碼。謝謝你的快速反應。 – 2011-12-16 17:17:52

+0

不用擔心,我們完全在同一時間做了我們的答案:) – Guillaume 2011-12-16 19:23:45

相關問題