2010-11-09 41 views
10

有沒有什麼方法可以顯示一個無模式對話框 - 一個對話框允許用戶在對話框之前與屏幕上的任何內容進行交互,但也允許用戶在按下時與對話框交互?定時非模態對話框

我知道Toasts,但他們不允許與彈出窗口進行交互。

我知道對話框,但它們是模態的,不允許與背景交互。

我知道通知,但我想要在屏幕上可見的東西。

我基本上想要能夠玩一個遊戲什麼的,彈出式窗口顯示我有一封新郵件或其他東西。我可以點擊它查看我的電子郵件,但如果我只想繼續玩我的遊戲,我可以等待它離開。這在Android中可能嗎?

+0

你想讓你的通知只出現在你的活動或你想它可能出現在其他的活動? – 2010-11-09 10:51:50

+0

只有我的活動 – 2010-11-13 03:17:52

回答

12

是的,創建一個活動,樣式爲Theme.Dialog。這是一個正常的活動,看起來像一個對話框,同時是無模式的並接受事件。

一個例子:

<activity android:name=".activity.dialog.PhotoDialog" 
      android:label="@string/photo_dialog_title" 
      android:theme="@android:style/Theme.Dialog"/> 

編輯

事實上Theme.Dialog模糊底層活動並使其不可訪問。我有一個類似的要求,我不得不顯示帶文本和取消按鈕的上傳進度對話框。主要的問題是設置WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL並重置WindowManager.LayoutParams.FLAG_DIM_BEHIND

創建一個對話框,使用自定義內容:

if (progressDialog == null) { 
      progressDialog = new Dialog(activityRequestingProgressDialog); 
      progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      progressDialog.setContentView(R.layout.progress_upload); 
      progressBar = (ProgressBar) progressDialog.findViewById(R.id.progressBar); 
      progressText = (TextView) progressDialog.findViewById(R.id.progressText); 
      progressText.setText("0 %"); 
      progressText.setTextSize(18); 
      Button buttonCancel = (Button) progressDialog.findViewById(R.id.btnCancel); 
      buttonCancel.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View view) { 
        cancelProgressDialog(); 
        stopUpload("Upload cancelled."); 
       } 
      }); 
      Window window = progressDialog.getWindow(); 
      window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, 
        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); 
      window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 
      window.setGravity(Gravity.BOTTOM); 
      progressDialog.show(); 
     } 

     progressText.setText(text); 
     progressBar.setProgress(percent); 

這是此對話框的佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/progressDialog" 
      android:orientation="vertical" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_centerVertical="true"> 

<TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:textSize="18sp" 
      android:padding="10dp" 
      android:text="@string/progress_title"/> 

<LinearLayout android:id="@+id/progressDialog" 
       android:orientation="horizontal" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:padding="10dp" 
       android:layout_centerVertical="true"> 

    <ProgressBar android:id="@+id/progressBar" 
       android:layout_width="150dp" 
       android:layout_height="34dp" 
       android:paddingRight="10dp" 
       android:max="100" 
       android:progress="0" 
       android:fadingEdge="vertical" 
       style="?android:attr/progressBarStyleHorizontal"/> 

    <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:id="@+id/progressText" 
       android:paddingRight="10dp"/> 

    <Button android:layout_height="40dp" 
      android:layout_width="80dp" 
      android:id="@+id/btnCancel" 
      android:text="@string/dialog_cancel"/> 

</LinearLayout> 
</LinearLayout> 
+0

這仍然允許與它後面的活動進行交互? – 2010-11-13 03:18:34

+0

是的,它允許它 – 2010-11-13 08:17:29

+0

真棒!我會試試這個! – 2010-11-14 11:18:21

0

我實現這是一個多一點的hackish,但也允許捕獲按鈕按下通過背景窗口

_wm = this.getWindowManager(); 
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
_view = layoutInflater.inflate(R.layout.notification, null); 

_params = new WindowManager.LayoutParams(); 
_params.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 
_params.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT; 
_params.flags = 
    // this is to keep button presses going to the background window 
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | 
    // this is to enable the notification to recieve touch events 
    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; 
// background transparant 
_params.format = PixelFormat.TRANSLUCENT; 
_gravity = Gravity.TOP;//Gravity.BOTTOM; 

_wm.addView(_view, _params); 
+0

可以正常工作...但我有這個對話框後面的列表視圖,我不能選擇任何listview elt ...任何建議,使listview元素可點擊? – fvisticot 2013-02-22 17:59:01

4

只需加上FLAG_NOT_TOUCH _MODAL標誌到您的對話框

dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);