在WebView中添加一个进度条
我正在尝试将进度/加载栏添加到使用WebView
应用程序中。 我很困惑如何实现每次单击链接时出现的进度条。
当前代码:
public class CULearnBrowser extends Activity { WebView webview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webview = (WebView) findViewById(R.id.webview); webview.setWebViewClient(new HelloWebViewClient()); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("https://culearn.colorado.edu/webct/entryPageIns.dowebct"); } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { webview.goBack(); return true; } return super.onKeyDown(keyCode, event); } }
活动布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
这个链接可能会帮助你。
我已经添加了几行代码,现在它的工作正常进度条。
getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main ); // Makes Progress bar Visible getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); webview = (WebView) findViewById(R.id.webview); webview.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { //Make the bar disappear after URL is loaded, and changes string to Loading... setTitle("Loading..."); setProgress(progress * 100); //Make the bar disappear after URL is loaded // Return the app name after finish loading if(progress == 100) setTitle(R.string.app_name); } }); webview.setWebViewClient(new HelloWebViewClient()); webview.getSettings().setJavaScriptEnabled(true); webview.loadUrl("http://www.google.com");
在你创buildWebview的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); pd = new ProgressDialog(SiteOpenInWebView.this); pd.setMessage("Please wait Loading..."); pd.show(); web_view.setWebViewClient(new MyWebViewClient()); web_view.loadUrl("ur site name"); }
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(); } } }
在这个方法中传递你的url
private void startWebView(String url) { WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setUseWideViewPort(true); webView.getSettings().setLoadWithOverviewMode(true); progressDialog = new ProgressDialog(ContestActivity.this); progressDialog.setMessage("Loading..."); progressDialog.show(); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { if (progressDialog.isShowing()) { progressDialog.dismiss(); } } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(ContestActivity.this, "Error:" + description, Toast.LENGTH_SHORT).show(); } }); webView.loadUrl(url); }
把一个进度条和webview放在一个relativelayout里面,并按如下方式设置进度条的属性,
- 使其能见度为GONE。
- CENTER它在Relativelayout中。
然后在webclient的onPageStarted()中使进度条可见,以便在点击链接时显示进度条。 在onPageFinished()使进度条visiblility作为GONE,以便它在页面完成加载时消失…这将适用于您的情况。 希望这可以帮助…
这是我正在使用的代码:
在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) { 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" />
希望这可以帮助..
你正在寻找的答案是非常详细的在这里:
最适合我的是
webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); mProgressBar.setVisibility(ProgressBar.VISIBLE); webView.setVisibility(View.INVISIBLE); } @Override public void onPageCommitVisible(WebView view, String url) { super.onPageCommitVisible(view, url); mProgressBar.setVisibility(ProgressBar.GONE); webView.setVisibility(View.VISIBLE); isWebViewLoadingFirstPage=false; } }
你可以尝试这个代码到你的活动
private void startWebView(WebView webView,String url) { webView.setWebViewClient(new WebViewClient() { ProgressDialog progressDialog; public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return false; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } public void onLoadResource (WebView view, String url) { if (progressDialog == null) { progressDialog = new ProgressDialog(SponceredDetailsActivity.this); progressDialog.setMessage("Loading..."); progressDialog.show(); } } public void onPageFinished(WebView view, String url) { try{ if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } }catch(Exception exception){ exception.printStackTrace(); } } }); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url); }
用这种方法调用这个方法:
startWebView(web_view,"Your Url");
有时如果URL死了,它将被redirect,并且会在onPageFinished方法之前到达onLoadResource()。 出于这个原因,进度条不会放弃。 要解决这个问题,请参阅我的答案 。
谢谢 :)