2011-09-09 67 views
1

我對Android編程頗爲陌生,所以請原諒我的愚蠢問題>,< 就像標題所說,我想在我的後面顯示AlertDialog/Toast ProgressDialog完成。我如何以正確的方式做到這一點?注意:我的處理程序僅用於解除ProgressDialog(pd.dismissDialog())。Android - 在ProgressDialog完成後顯示AlertDialog/Toast

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.btnDoRegister: 
    pd = ProgressDialog.show(RegisterActivity.this, "", 
        "Registering with the server.."); 
      new Thread() { 
       public void run() { 
        Looper.prepare(); 
        //(hopefully) thread-safe implementations here 
        handler.sendEmptyMessage(0); 
       } 
      }.start(); 
      if (status == registered) { 
       //show AlertDialog/Toast here 
       finish(); 
      } else if (status == notRegistered) { 
       //show AlertDialog/Toast here 
      } 
    break; 
    } 

更重要的是混亂的是,當我試圖調試此,logcat的說明不了什麼,一切都運行完美(警報/敬酒顯示爲預期)。但是當我試圖正常運行時,不知何故,我覺得這運行得太快,並沒有正確顯示我的警報/吐司(聽起來很愚蠢)。非常感謝您的幫助。

+0

k,在哪個線程中您解散進度對話框?只是顯示那個coe。 – user370305

回答

1

這將是更適合顯示AlertDialog /吐司的處理程序的handleMessage方法,

+0

你說得對。在線程處理程序中調用AlertDialog/Toast之後,它正常工作!非常感謝你的幫助 :) –

0

發生這種情況的原因是,您的線程在運行時很快就會完成其工作。但是當你像其他過程語言一樣調試它時,你需要依次執行一些操作。但如果你仍然想它顯示對話框,很長一段時間,做到這一點(雖然沒有實際用處的)

new Thread() { 
       public void run() { 
        Looper.prepare(); 
        //(hopefully) thread-safe implementations here 
        handler.sendEmptyMessage(0); 
        Thread.sleep(3000); 
       } 
      }.start(); 
相關問題