2011-06-28 47 views
1

我可以顯示ProgressDialog,但加載WebView後無法停止。我需要的只是一個簡單的刷新按鈕。加載WebView後無法停止ProgressDialog

這裏是工作的代碼至今:

 public class Quotes extends Activity implements OnClickListener{ 

     WebView webview; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     webview = (WebView) findViewById(R.id.scroll); 
     webview.getSettings().setJavaScriptEnabled(true); 
     webview.loadUrl("http://www.dgdevelco.com/quotes/quotesandroid.html"); 

     View refreshClick = findViewById(R.id.refresh); 
     refreshClick.setOnClickListener(this); 

     } 


     public void onClick(View v){ 
      switch(v.getId()){ 

      case R.id.refresh: 
       ProgressDialog.show(Quotes.this, "", "Loading...", true); 
       webview.reload(); 

       } 
      } 
     } 

我只是不明白。我到處尋找,但似乎沒有任何工作。

+0

有數字下勾選答案左側的輪廓。按那個。這是你如何標記一個答案正確,並得到你的上癮的道路StackOverflow ;-) – Blundell

+0

從相關部分的權利:http://stackoverflow.com/questions/3017375/android-showing-a-progress-對話框http://stackoverflow.com/questions/3283819/how-do-i-make-my-progress-dialog-dismiss-after-webview-is-loaded http://stackoverflow.com/questions/4388142/how- do-i-show-and-then-remove-an-android-progress-dialog http://stackoverflow.com/questions/4395411/android-progress-dialog-not-displaying還有很多很多...... –

回答

5

這是快速的答案,你應該真的爲WebViewClient實現你自己的類,你也應該在該類中顯示對話框,但你會弄清楚。

首先,讓你的對話框成爲全局對象(在你的真實應用程序中,你可能想把它傳遞給你的客戶端,或者在webviewclient中聲明它,然後重寫onPageStarted)。

ProgressDialog dialog;  

後來乾脆:

webview.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      dialog.dismiss(); 
     } 
    }); 



    public void onClick(View v){ 
     switch(v.getId()){ 

     case R.id.refresh: 
      dialog = ProgressDialog.show(Quotes.this, "", "Loading...", true); 
      webview.reload(); 

      } 
     } 
    } 

這是你需要的API:WebViewClient

編輯

確定它是討厭我,這裏是正確的方法:

public class Quotes extends Activity implements OnClickListener{ 

    private WebView webview; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    webview = (WebView) findViewById(R.id.scroll); 
    webview.getSettings().setJavaScriptEnabled(true); 
    webview.setWebViewClient(new MyWebViewClient(this)); 
    webview.loadUrl("http://www.dgdevelco.com/quotes/quotesandroid.html"); 

    findViewById(R.id.refresh).setOnClickListener(this); 
    } 


    public void onClick(View v){ 
     switch(v.getId()){ 
     case R.id.refresh: 
      webview.reload(); 
      break; 
      } 
     } 
    } 

新類:

public class MyWebViewClient extends WebViewClient { 
    private Context mContext; 
    private ProgressDialog mDialog; 

    public MyWebViewClient(Context context){ 
    mContext = context; 
    } 

    @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      super.onPageStarted(view, url, favicon); 
      mDialog = ProgressDialog.show(mContext, "", "Loading...", true); 
     } 

    @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      mDialog.dismiss(); 
     } 

} 
+0

有效!!!你真是太棒了!謝謝soooooo soooooo!我知道它必須在那裏。你這麼搖滾!謝謝你,謝謝你,謝謝你。 :) – Chris