2017-05-28 21 views
0

我正在顯示服務中的AlertDialog。問題是,我想在我的AlertDialog中添加ProgressBar(圓形)。我該怎麼做呢?我試圖用ProgressDialog替換AlertDialog,但它會強制關閉,我知道這是不正確的方式反正。如何在我的alertdialog中添加一個圓形的進度條?

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
      builder.setTitle("APP IS OBSOLETE") 
        .setMessage("To continue, please wait for the updates to download.") 
        .setCancelable(false) 
        .setPositiveButton("no, thanks", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          System.exit(0); 
         } 
        }); 
      AlertDialog alert = builder.create(); 
      alert.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 
      alert.show(); 

PS:我要在服務中做到這一點,而無需使用任何額外的佈局。

+0

服務不應該顯示對話框。我很驚訝它甚至爲你工作。無論如何,用戶界面無論如何都不會是主題。相反,我建議你展示一個通知,它可能會啓動一個對話主題活動。 – BladeCoder

+0

@BladeCoder難怪你會這麼說。你看的樣子。我可以告訴你很聰明:) – user8069029

回答

0

您仍然應該使用AlertDialog,但更改它顯示的View

https://developer.android.com/reference/android/app/Dialog.html#setContentView(android.view.View)

 AlertDialog.Builder builder = new AlertDialog.Builder(context); 
     builder.setView(R.layout.my_progress_view) 
     ... 

使用的建設者這種方法:https://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(int)

R.layout.my_progress_view是包含XML文件的ProgressBar(你創建你的活動創建佈局方法相同)

+0

有一個挑戰。我只想通過單一服務來完成此操作,而無需額外的設計佈局 – user8069029

+0

然後以編程方式創建View並使用:https://developer.android.com/reference/android/app/AlertDialog.Builder.html#setView(android。 view.View) 我已經回答了你的問題 - 你的評論是一個新問題 – Blundell