您的位置:首页 > 移动开发

WebView显示网页

2016-04-21 19:43 344 查看
可以通过Intent调用系统浏览器

Uri uri=Uri.parse(url); //url为要访问的地址

Itent intent = new Intent(Intent.ACTION_VIEW,uri);

startActivity(intent);

XML:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bcp.webviewtest.MainActivity">

<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView" />
</RelativeLayout></span>


JAVA:
package com.bcp.webviewtest;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/***
* 1、将WebView加入你的应用
* 2、使用WebView加载页面
*
*  在WebView加载页面要使用loadUrl()
*  web资源:webView.loadUrl("http://www.baidu.com");
*  本地文件用:webView.loadUrl("file:///android_assets/XXX.html");
*  注: 本地文件存在于assets文件夹中
*  使页面获得焦点: webView.requestFocus();
*
* 3、获取网络访问权限
*      在它有效工作之前,要保证你的应用能访问网络,要访问网络,
*      需要在配置文件中获取INTERNET权限
*      <manifest....>
*          <uses-permission android:name="android.permission.INTERNET"/>
*          ...
*      </manifest>
* 4、在WebView中使用Javascript
*      如果想要在WebView中加载的web页面使用JS,需要在WebView中启动JS,
*      可以通过WebView自带的WebSettings来启动。通过getSettings()获取WebSettings的值
*      再通过setJavaScriptEnabled()来启动Javascript
*
*      WebView webView = (WebView)FindViewById(R.id.webView);
*      WebSettings webSettings = webView.getSettings();
*      webSettings.setJavaScriptEnabled(true);
* 5、处理页面导航
*      当用户点击一个WebView中的页面的链接时,通常是用默认的浏览器打开的。
*      可以在WebView中覆盖这个行为,链接就会在WebView中打开
*      webView.setWebViewClient(new WebViewClient());
* 6、后退和前进
*      当WebView覆盖了URL加载,它会自动生成历史访问记录,
*      可以通过goBack()或goForward()实现后退或前进
*      @Override
*      public boolean onKeyDown(int keyCode,KeyEvent event){
*          if((keyCode==KeyEvent.KEYCODE_BACK)&&myWebView.canGoBack()){
*              webView.goBack();
*              return true;
*          }
*          return super.onKeyDown(keyCode,event);
*      }
* 7、判断页面加载过程
*      某些网页加载缓慢时,需要判断加载过程,制作进度条来进行显示
*      webView.setWebChromeClient(){
*          @Override
*          public void onProgressChanged(WebView view,int newProgress){
*              if(newProgress==100){
*                  //加载完成
*
*              }else{
*                  //加载中
*              }
*          }
*      }
* 8、WebView缓存的运用
*      优先使用缓存:
*      webView.getSetings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
*      不使用缓存:
*      webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
*/
public class MainActivity extends AppCompatActivity {
private String url="http://www.baidu.com";
private WebView webView;
//判断网页加载过程
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//        Uri uri=Uri.parse(url); //url为要访问的地址
//
//        Intent intent = new Intent(Intent.ACTION_VIEW,uri);
//
//        startActivity(intent);

init();

}
//初始化函数
private void init() {
webView= (WebView) findViewById(R.id.webView);
//加载页面
webView.loadUrl("http://www.baidu.com");
//覆盖默认通过第三方或者系统浏览器打开网页的行为
//使网页可以在WebView中打开
webView.setWebViewClient(new WebViewClient(){
//重写默认方法
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//返回true时在WebView中打开,false在系统或第三方浏览器中打开
view.loadUrl(url);
return true;
}
//WebViewClient帮助WebView去处理一些页面控制和请求通知

});

//启用J支持S
WebSettings webSettings=webView.getSettings();

webSettings.setJavaScriptEnabled(true);
//加载页面优先使用缓存
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

//设置进度条
webView.setWebChromeClient(new WebChromeClient(){
//判断当前页面加载进度
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
//newProgress返回1--100的整数
if(newProgress==100){
//加载完毕
closeDialog();
}else{
//加载中,打开ProgressDialog
openDialog(newProgress);
}
}
private void openDialog(int newProgress) {
if(progressDialog==null){
progressDialog=new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正在加载");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(newProgress);
progressDialog.show();
}else{
progressDialog.setProgress(newProgress);
}
}

private void closeDialog() {
if(progressDialog!=null&&progressDialog.isShowing()){
progressDialog.dismiss();
progressDialog=null;
}
}
});
}

//改写物理按键----返回的逻辑

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//点击了后退物理按键
if (keyCode==KeyEvent.KEYCODE_BACK){
//判断是否有网页缓存可以后退
if(webView.canGoBack()){
webView.goBack();//执行后退方法
return true;
}else{
System.exit(0);//没有网页缓存可后退就直接退出程序
}
}
return super.onKeyDown(keyCode, event);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: