example.html文件的位置
以下是代码
example.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>实例</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<a href="http://www.baidu.com">打开百度</a>
</body>
</html>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.webview">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lenovo.webview.MainActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
MainActivity
package com.example.lenovo.webview;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView webView;
private String url="http://2014.qq.com/";
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(WebView)findViewById(R.id.webview);
init();
}
private void init() {
//WebView加载本地资源
//webView.loadUrl("file:///android_asset/example.html");
//webView加载web资源
webView.loadUrl(url);
//覆盖WebView请求系统浏览器或者第三方浏览器打开网页,在WebView自己里面打开
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//返回true在自己里面打开,返回false在第三方或者系统浏览器中打开
view.loadUrl(url);
return true;
}
});
//WebViewClient帮助webview处理一些页面的控制和请求通知
//启用JavaScript
WebSettings settings=webView.getSettings();
settings.setJavaScriptEnabled(true);
//webView优先加载缓存
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//打开一个网页是加载一个对话框,提示用户当前加载了白分之几
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
//newProgress是1%-100%
if(newProgress==100){
closedialogue();
}
else{
openDialogue(newProgress);
}
}
public void closedialogue(){
if(dialog!=null&&dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
}
private void openDialogue(int newProgress){
if(dialog==null){
dialog=new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
}
else
dialog.setProgress(newProgress);
}
});
}
//改写物理按键——返回的逻辑
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK)
{
Toast.makeText(this,webView.getUrl(),Toast.LENGTH_SHORT).show();
if(webView.canGoBack())
{
webView.goBack();
return true;
}
else
System.exit(0);//程序退出
}
return super.onKeyDown(keyCode, event);
}
}