2012-09-18 57 views
0

我遇到了無法隱藏的警報對話框的問題。如何在AlertDialog後顯示ProgressDialog

當用戶按下一個按鈕,我證明與此代碼創建一個對話框:

new AlertDialog.Builder(this) 
      .setTitle(R.string.enterPassword)     
      .setView(textEntryView)   
      .setPositiveButton(R.string.ok, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          String password = pwdText.getText().toString(); 
          dialog.dismiss(); 
          processUserAction(password,targetUri); 
         } 
        }) 
      .setNegativeButton(R.string.cancel, 
        new DialogInterface.OnClickListener() {      
         public void onClick(DialogInterface dialog, int whichButton) { 
          }     
         })    
      . 
      create(); 

裏有「processUserAction」的方法進行了一些沉重的操作,和裏面我使用的是AysncTask那顯示一個ProgressDialog。

我遇到的問題是,對話框提示輸入密碼永遠不會出現在屏幕上(我試過用dismiss(),cancel())。 我想它一直呆在那裏,直到onClick方法完成。

所以,我的問題是如何關閉AlertDialog,所以我可以顯示ProgressDialog?

我一直在嘗試的另一種方法是在AlertDialog中設置DismissListener並從那裏調用繁重的操作,但是我沒有運氣(它沒有被調用)。

編輯:添加的AsyncTask代碼

public class BkgCryptOperations extends AsyncTask<File,Void,Integer>{ 

    @Override 
    protected Integer doInBackground(File... files) { 
     if (files!=null && files.length > 0){ 
      File source = files[0]; 
      File target = files[1]; 
      return cryptAction.process(source,password, target); 
     } 

     return Constants.RetCodeKO; 
    } 

    CryptAction cryptAction; 
    String password; 
    ProgressDialog progressDialog; 


    public BkgCryptOperations (CryptAction cryptAction,String password,ProgressDialog progressDialog){ 
     this.cryptAction=cryptAction; 
     this.password=password; 
     this.progressDialog=progressDialog; 
    } 

    @Override 
    protected void onPreExecute() { 
     if (progressDialog!=null){ 
      progressDialog.show(); 
     } 
    } 
    protected void onPostExecute(Integer i) { 
     if (progressDialog!=null){ 
      progressDialog.dismiss(); 
     } 
    } 


} 

在此先感謝

+1

是你調用dialog.show()?當然,你必須調用它來顯示一個對話框。如果是,您是否可以修改代碼以獲得更多清晰度。 – VendettaDroid

+0

你可以展示你的asynctask嗎? – wesdfgfgd

+0

是的,我創建它後立即調用dialog.show()。你想讓我澄清哪一部分?我想我的setPositiveButton onClickListener部分有問題。 – richardtz

回答

1

這裏是一個excample我如何做到這一點:

public void daten_remove_on_click(View button) { 
     // Nachfragen 
     if (spinadapter.getCount() > 0) { 
      AlertDialog Result = new AlertDialog.Builder(this) 
        .setIcon(R.drawable.icon) 
        .setTitle(getString(R.string.dialog_data_remove_titel)) 
        .setMessage(getString(R.string.dialog_data_remove_text)) 
        .setNegativeButton(getString(R.string.dialog_no), 
          new DialogInterface.OnClickListener() { 
           public void onClick(
             DialogInterface dialogInterface, int i) { 
            // Nicht löschen 
            dialogInterface.cancel(); 
           } 
          }) 
        .setPositiveButton(getString(R.string.dialog_yes), 
          new DialogInterface.OnClickListener() { 
           public void onClick(
             DialogInterface dialogInterface, int i) { 
            String _quellenName = myCursor.getString(1); 
            deleteQuellenRecord(_quellenName); 
            zuletztGelöscht = _quellenName; 
           } 
          }).show(); 
     } else { 
      // Keine Daten mehr vorhanden 
      Toast toast = Toast.makeText(Daten.this, 
        getString(R.string.dialog_data_remove_empty), 
        Toast.LENGTH_SHORT); 
      toast.show(); 
     } 
    } 

這裏是deleteQuellenRecord的代碼:

private void deleteQuellenRecord(String _quellenName) { 
     String DialogTitel = getString(R.string.daten_delete_titel); 
     String DialogText = getString(R.string.daten_delete_text); 
     // Dialogdefinition Prograssbar 
     dialog = new ProgressDialog(this) { 
      @Override 
      public boolean onSearchRequested() { 
       return false; 
      } 
     }; 
     dialog.setCancelable(false); 
     dialog.setTitle(DialogTitel); 
     dialog.setIcon(R.drawable.icon); 
     dialog.setMessage(DialogText); 
     // set the progress to be horizontal 
     dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     // reset the bar to the default value of 0 
     dialog.setProgress(0); 
     // set the maximum value 
     dialog.setMax(4); 
     // display the progressbar 
     increment = 1; 
     dialog.show(); 
     // Thread starten 
     new Thread(new MyDeleteDataThread(_quellenName)) { 
      @Override 
      public void run() { 
       try { 
        // Datensatz löschen 
        myDB.execSQL("DELETE ... ');"); 
        progressHandler 
          .sendMessage(progressHandler.obtainMessage()); 
        myDB.execSQL("DELETE ...);"); 
        // active the update handler 
        progressHandler 
          .sendMessage(progressHandler.obtainMessage()); 
        myDB.execSQL("DELETE ...;"); 
        // active the update handler 
        progressHandler 
          .sendMessage(progressHandler.obtainMessage()); 
        // Einstellung speichern 
        try { 
         settings.edit().putString("LetzteQuelle", "-1") 
           .commit(); 
        } catch (Exception ex) { 
         settings.edit().putString("LetzteQuelle", "").commit(); 
        } 

       } catch (Exception ex) { 
        // Wait dialog beenden 
        dialog.dismiss(); 
        Log.e("Glutenfrei Viewer", 
          "Error in activity MAIN - remove data", ex); // log 
                      // the 
                      // error 
       } 
       // Wait dialog beenden 
       dialog.dismiss(); 

      } 
     }.start(); 
     this.onCreate(null); 
    } 

室內用異步任務我做這種方式:

private class RunningAlternativSearch extends 
      AsyncTask<Integer, Integer, Void> { 


     final ProgressDialog dialog = new ProgressDialog(SearchResult.this) { 
      @Override 
      public boolean onSearchRequested() { 
       return false; 
      } 
     }; 



     @Override 
     protected void onPreExecute() { 
      alternativeSucheBeendet = false; 
      String DialogTitel = getString(R.string.daten_wait_titel); 
      DialogText = getString(R.string.dialog_alternativ_text); 
      DialogZweiteChance = getString(R.string.dialog_zweite_chance); 
      DialogDritteChance = getString(R.string.dialog_dritte_chance); 
      sucheNach = getString(R.string.dialog_suche_nach); 
      dialog.setCancelable(true); 
      dialog.setTitle(DialogTitel); 
      dialog.setIcon(R.drawable.icon); 
      dialog.setMessage(DialogText); 
      dialog.setOnDismissListener(new OnDismissListener() { 
       public void onDismiss(DialogInterface arg0) { 
        // TODO Auto-generated method stub 
        cancleBarcodeWorker(); 
        if (alternativeSucheBeendet==false){ 
         // Activity nur beenden wenn die Suche 
         // nicht beendet wurde, also vom User abgebrochen 
         Toast toast = Toast.makeText(SearchResult.this, SearchResult.this 
           .getString(R.string.toast_suche_abgebrochen), 
           Toast.LENGTH_LONG); 
         toast.show(); 
         myDB.close(); 
         SearchResult.this.finish(); 
        } 
       } 
      }); 
      dialog.show(); 
     } 


     ... 
+0

和Aaron的建議一樣,我會嘗試並更新,但乍一看我看不出差異,因爲onClink方法在處理繁重操作時仍會執行。感謝您的建議。 – richardtz

+0

我已經更新了答案。 deleteQuellenRecord是一個長時間運行的任務,顯示一個進度條。奇怪的是,我從來沒有調用Result.dismiss()或Result.cancle(),但是Dialog消失並且進度條出現在前面。也許它有助於刪除行dialog.dismiss()或將其更改爲cancle() –

+0

我沒有dismiss()或cacncel()之前,我只是把它放在那裏,因爲我讀到他們被推遲工作。你是如何執行deleteQuellenRecord中的長時間運行任務的?使用AsyncTask?或者只是創建自己的線程? – richardtz

0

您可以創建AlertDialog之外的聽衆,抽象出了積極的按鈕OnClickListener中的邏輯。這樣,可以通知監聽者,AlertDialog將立即被解除。然後,AlertDialog的用戶輸入的任何處理都可以獨立於AlertDialog進行。我不確定這是否是實現這一目標的最佳方式,但過去對我來說效果很好。

據我所知,我沒有看到任何明顯的問題與您的AsyncTask代碼。

public interface IPasswordListener { 
    public void onReceivePassword(String password); 
} 

IPasswordListener m_passwordListener = new IPasswordListener { 
    @Override 
    public void onReceivePassword(String password) { 
     processUserAction(password,targetUri); 
    } 
} 

public void showPasswordDialog() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(R.string.enterPassword); 
    builder.setView(textEntryView); 
    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      m_passwordListener.onReceivePassword(pwdText.getText().toString()); 
      dialog.dismiss(); 
     } 
    }); 
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      dialog.cancel(); 
     } 
    }); 
    builder.show(); 
} 
+0

我會嘗試並更新,但乍一看我看不到差異,因爲onClink方法在處理繁重的操作時仍會執行。感謝您的建議。 – richardtz

1

可以顯示processUserAction(..)的代碼嗎?沒有必要包括解僱。 我做了非常類似的東西,也沒有問題...... 下面的代碼:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Export data.\nContinue?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          String file = getObra().getNome(); 
          d = new ProgressDialog(MenuActivity.this); 
          d.setTitle("Exporting..."); 
          d.setMessage("please wait..."); 
          d.setIndeterminate(true); 
          d.setCancelable(false); 
          d.show(); 
          export(file); 
         } 
        }) 
      .setNegativeButton("No", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 

在出口(文件),我打開線程:

private void export(final String file) { 


    new Thread() { 
     public void run() { 
      try { 
       ExportData ede = new ExportData(
         getApplicationContext(), getPmo().getId(), 
         file); 
       ede.export(); 
       handlerMessage("Done!!"); 
      } catch (Exception e) { 
       handlerMessage(e.getMessage()); 
       System.out.println("ERROR!!!" + e.getMessage()); 
      } 
     } 
    }.start(); 
} 

在handlerMessage我駁回progressDialog和表演最後的消息。 希望它可以幫助你。

+0

我可以添加processUserAction方法,但它只會混淆事物。它工作正常,但在執行4-5秒內,我只能看到應用程序頂部的AlertDialog(這是我的問題,它不太友好)。在這段時間它也是無法點擊的。你提出的方法與我使用的AsyncTask不同,我會嘗試並更新。感謝您的建議。 – richardtz

+0

它使用這種方法。非常感謝你的幫助。 – richardtz