2014-12-30 108 views
-1

我正在製作一個web視圖,並試圖顯示使用進度加載消息Dialog.progress對話框顯示,但不是在URL的所有內容加載後解僱。請幫助我。ProgressDialog不是解僱

mWebview = new WebView(this); 
    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript 
    final Activity activity = this; 
    mWebview.setWebViewClient(new WebViewClient() { 
     ProgressDialog prDialog; 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      prDialog = ProgressDialog.show(Web.this, null, "loading, please wait..."); 
      super.onPageStarted(mWebview, url, favicon); 
      Toast.makeText(getApplicationContext(), "started!"+1, Toast.LENGTH_SHORT).show(); 
      num++; 
     } 
     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(mWebview, url); 
      prDialog.dismiss(); 
      Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show(); 
     } 
     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); 
     } 
    }); 


    mWebview .loadUrl("http://www.google.com"); 
    setContentView(mWebview); 
+2

'onPageFinished'被調用嗎?你看到「完成」烤麪包了嗎? –

+0

是... @ jason g peterson –

+0

是的,它顯示「完成」吐司消息。 –

回答

1
package com.example.webviewtag; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

public class WebViewDemo extends Activity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    WebView webView = new WebView(this); 
    webView.setClickable(true); 
    webView.setFocusableInTouchMode(true); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://www.google.com"); 
    WebClientClass webViewClient = new WebClientClass(); 
    webView.setWebViewClient(webViewClient); 
    WebChromeClient webChromeClient=new WebChromeClient(); 
    webView.setWebChromeClient(webChromeClient); 
    setContentView(webView); 
} 

public class WebClientClass extends WebViewClient { 
    ProgressDialog pd = null; 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
    super.onPageStarted(view, url, favicon); 
    pd = new ProgressDialog(WebViewDemo.this); 
    pd.setTitle("Please wait"); 
    pd.setMessage("Page is loading.."); 
    pd.show(); 
    } 

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

public class WebChromeClass extends WebChromeClient{ 
} 
} 
0

試試這個代碼,

private class MYWEBCLIENT extends WebViewClient { 

    private ProgressDialog prDialog; 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) { 
     super.onPageStarted(view, url, favicon); 
     prDialog = ProgressDialog.show(activity, "", "Please wait..."); 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
     super.onPageFinished(view, url); 
     if (prDialog != null && prDialog.isShowing()) 
      prDialog.dismiss(); 
    } 
} 

加載web視圖代碼,

webViewInfo.getSettings().setJavaScriptEnabled(true); 
webViewInfo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
webViewInfo.setWebViewClient(new MYWEBCLIENT()); 
webViewInfo.loadData("YOUR_URL_OR_HTML_FILE", "text/html", "UTF-8"); 
+0

謝謝,同樣的問題......連續顯示加載對話框。 –

+0

你好,試試這個代碼,我希望你的解決方案。 –

0

你可以試試這個java代碼:

就需要更換我的實體你的。如類名或其他變量或東西。

public class WebViewDemo extends Activity 
{ 

    WebView mWebview; 
    ProgressDialog prDialog; 

    @SuppressLint("SetJavaScriptEnabled") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.web); 

    mWebview = (WebView) findViewById(R.id.your_id); 
    mWebview.getSettings().setJavaScriptEnabled(true); 

    CustomWebClient webClient =new CustomWebClient(WebViewDemo.this); 
    mWebview.setWebViewClient(webClient); 
    mWebview .loadUrl("http://www.google.com"); 

} 

// This is your custom webviewclient 

public class CustomWebClient extends WebViewClient 
{ 
    public Context context; 

    public CustomWebClient(Context context) 
    { 
     // TODO Auto-generated constructor stub 
     this.context = context; 
     prDialog = new ProgressDialog(context); 
     prDialog.setMessage("loading please wait..."); 
     prDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    } 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap favicon) 
    { 
     // TODO Auto-generated method stub 
     super.onPageStarted(view, url, favicon); 
     prDialog.show(); 
    } 

    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // TODO Auto-generated method stub 
     view.loadUrl(url); 
     return true; 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) 
    { 
     // TODO Auto-generated method stub 
     super.onPageFinished(view, url); 
     try 
     { 
      if ((prDialog != null) && prDialog.isShowing()) 
      { 
       prDialog.dismiss(); 
      } 
     } catch (final IllegalArgumentException ae) { 
     } catch (final Exception excep) { 
     } finally { 
      prDialog = null; 
     } 

    } 

    @Override 
    public void onReceivedHttpAuthRequest(WebView view, 
      HttpAuthHandler handler, String host, String realm) 
    { 
     // TODO Auto-generated method stub 
     super.onReceivedHttpAuthRequest(view, handler, host, realm); 
    } 

    @Override 
    public void onReceivedError(WebView view, int errorCode, 
      String description, String failingUrl) 
    { 
     // TODO Auto-generated method stub 
     super.onReceivedError(view, errorCode, description, failingUrl); 
    } 
    } 
} 

你可以運行我的java類來測試,它是否在你的最後工作。

希望這可以幫助你。

+0

請嘗試讓我知道它是否適合你。 –