如何在webview上显示进度条?

我正在尝试使用WebView在我的应用程序中打开一个网页。 当我打开网页时,它会显示一段时间的空白屏幕,然后在我的应用程序的浏览器中打开该页面。

任何人都build议我如何显示进度或摆脱加载webview期间出现的空白屏幕?

我正在使用以下代码:

 WebView mWebView = (WebView) findViewById(R.id.mywebview); mWebView.getSettings().setJavaScriptEnabled(true); // error handling final Activity activity = this; mWebView.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); } }); // error handling mWebView.loadUrl(URL); mWebView.setWebViewClient(new HelloWebViewClient()); 

Solutions Collecting From Web of "如何在webview上显示进度条?"

尝试这个:

 getWindow().requestFeature(Window.FEATURE_PROGRESS); WebView mWebView = (WebView) findViewById(R.id.mywebview); mWebView.getSettings().setJavaScriptEnabled(true); final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient(){ public void onProgressChanged(WebView view, int progress) { activity.setTitle("Loading..."); activity.setProgress(progress * 100); if(progress == 100) activity.setTitle("My title"); } }); mWebView.loadUrl(URL); 

这是我正在使用的代码:

在WebViewClient里面:

 webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); findViewById(R.id.progress1).setVisibility(View.VISIBLE); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); findViewById(R.id.progress1).setVisibility(View.GONE); } }); 

这里是XML:

 <ProgressBar android:id="@+id/progress1" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 

希望这可以帮助..

你需要做这样的事情:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_PROGRESS); this.setProgressBarVisibility(true); } 

接着

 final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setProgress(progress * 100); } }); 

更新
我知道这有点太晚了,但我有一点小小的注意:你不应该使用setWebViewClient()两次。 第二次呼叫取消了第一个呼叫,所以你不会得到error handling。

1.在创build你的电话AsyncTask

2.在asynctask中,你只需要进行对话并显示进度对话框。

3.在webview客户端ü只是再次显示进度对话框点击网站的任何链接打开你的web 视图和完整的负载链接后,我们覆盖方法 onPageFinished和在这个方法中,我们closures进度对话框。

在OnCreate

 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.web_view); web_view = (WebView) findViewById(R.id.web_view); OpenWebSiteInWebView opensite = new OpenWebSiteInWebView(); opensite.execute(); } 

的AsyncTask

 private class OpenWebSiteInWebView extends AsyncTask<String, Void, String> { @SuppressWarnings("deprecation") @SuppressLint("SetJavaScriptEnabled") @Override protected String doInBackground(String... params) { web_view.setWebViewClient(new MyWebViewClient()); web_view.loadUrl("ur site name"); return null; } @Override protected void onPostExecute(String result) { } @Override protected void onPreExecute() { pd = new ProgressDialog(SiteOpenInWebView.this); pd.setMessage("Please wait Loading..."); pd.show(); } } 

WebViewClient

 private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); if (!pd.isShowing()) { pd.show(); } return true; } @Override public void onPageFinished(WebView view, String url) { System.out.println("on finish"); if (pd.isShowing()) { pd.dismiss(); } } } 

这里是我在KOTLIN中使用的代码:

 private fun setupWebView() { val webViewClient: WebViewClient = object: WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { view?.loadUrl(request?.url.toString()) return super.shouldOverrideUrlLoading(view, request) } override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { showProgressDialog() super.onPageStarted(view, url, favicon) } override fun onPageFinished(view: WebView?, url: String?) { hideProgressDialog() super.onPageFinished(view, url) } } webView.webViewClient = webViewClient webView.settings.javaScriptEnabled = true webView.settings.defaultTextEncodingName = "utf-8" }