2014-12-30 78 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

请尝试让我知道它是否适合你。 –