progressbar
问题:如何使用ProgressBar加载WebView?
描述:之前,我曾发表过一篇针对Android的文章-WebViewClient示例,其中我们讨论了如何在应用程序内加载URL而不是打开本机浏览器。
现在,考虑一下要包含进度条以显示加载过程的情况。 同时,我们可以显示ProgressDialog或在XML布局中包含ProgressBar。–如果包含ProgressBar,则每当页面加载完成时就必须使其不可见或消失–并且,如果我们显示ProgressDialog,则每当页面加载完成时就必须将其关闭。因此,在本教程中,我们将显示ProgressBar。 只需检查snap-2,其中ProgressBar是不可见的,因为我们已使其不可见。
输出:
注意:在实施此解决方案之前,请确保已在AndroidManifest.xml文件中添加了INTERNET权限。
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
解:WebViewClientDemoActivity.java
package com.paresh.webviewclientdemo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
/*
* Demo of creating an application to open any URL inside the application and clicking on any link from that URl
should not open Native browser but that URL should open in the same screen.
- Load WebView with progress bar
*/
public class WebViewClientDemoActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("http://www.technotalkative.com");
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@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);
progressBar.setVisibility(View.GONE);
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
main.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="This is the demo of WebView Client" android:textsize="20sp" android:gravity="center_horizontal">
</textview>
<progressbar android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:id="@+id/progressBar1">
<webview android:id="@+id/webview01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="1">
</webview>
</progressbar></linearlayout>
从此处下载完整的源代码: Android –使用ProgressBar加载WebView。
反馈和评论总是受欢迎的。 请
参考: Android –从我们的JCG合作伙伴加载带有ProgressBar的WebView 在TechnoTalkative博客上的Paresh N. Mayani 。
翻译自: https://www.javacodegeeks.com/2012/03/android-load-webview-with-progressbar.html
progressbar