在开发中经常在更多页面中用到WebView来显示一个网页,这个网页里面放有下载地址或者链接到别的网页等等。
效果如下:
这个示例很简单,代码中都有注释。下面直接上代码:
public class MainActivity extends Activity { private static final String URL = "http://demo.91yong.com/mobile/moreApp.jsp?type=2"; private WebView wvShow; private ProgressBar pbLoading; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wvShow = (WebView) findViewById(R.id.web_view); pbLoading = (ProgressBar) findViewById(R.id.pb_loading); wvShow.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { // 监听下载功能,当用户点击下载链接的时候,直接调用系统的浏览器来下载 Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); } }); wvShow.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { //页面加载完毕 super.onPageFinished(view, url); if (pbLoading != null && pbLoading.getVisibility() == View.VISIBLE) { pbLoading.setVisibility(View.GONE);//进度条不可见 } } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { //页面开始加载 super.onPageStarted(view, url, favicon); if (pbLoading != null && pbLoading.getVisibility() == View.INVISIBLE) { pbLoading.setVisibility(View.VISIBLE);//进度条可见 } } }); wvShow.getSettings().setJavaScriptEnabled(true);//设置支持脚本 wvShow.getSettings().setBuiltInZoomControls(true);// 设置支持缩放 wvShow.getSettings().setDefaultZoom(ZoomDensity.FAR);// 屏幕自适应网页,如果没有这个,在低分辨率的手机上显示可能会异常 wvShow.loadUrl(URL); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.more_app, menu); return true; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.more_app, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.refresh: {// 刷新 wvShow.loadUrl(URL); break; } case R.id.open_in_brower: {// 在浏览器中打开 Uri uri = Uri.parse(URL); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); break; } } return true; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <WebView android:id="@+id/web_view" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <!-- 正在加载的提示 --> <ProgressBar android:id="@+id/pb_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" style="?android:attr/progressBarStyle" /> </RelativeLayout>
监听了菜单按钮:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/refresh" android:title="刷新" ></item> <item android:id="@+id/open_in_brower" android:title="在浏览器中打开" ></item> </menu>
最后当然不要忘记权限:
<uses-permission android:name="android.permission.INTERNET" />
项目地址:
https://github.com/michaelye/WebViewDemo