一.创建Activity,layout文件
activity_web_view.xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_web_view" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <WebView android:id="@+id/myWebView" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
WebViewActivity文件的代码
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.app.NotificationCompatSideChannelService; import android.view.KeyEvent; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class WebViewActivity extends Activity { private WebView myWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); myWebView=(WebView)findViewById(R.id.myWebView); //WebView加载网页 //1:加载assets文件下网页 //myWebView.loadUrl("file:///android_asset/Hello.html"); //2:加载sdcard上指定网页 //myWebView.loadUrl("content://com.android.htmlfileprovider/sdcard/test.html"); //3:加载Web网页 //myWebView.loadUrl("http://www.baidu.com"); myWebView.loadUrl("http://192.168.8.25:8080/Web_Project/");//自己创建的网页链接 //WebView常见设置 myWebView.setWebViewClient(new WebViewClient(){ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); //Toast.makeText(WebViewActivity.this, "网页加载中.....", Toast.LENGTH_SHORT).show(); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); //Toast.makeText(WebViewActivity.this, "网页加载完毕", Toast.LENGTH_SHORT).show(); } }); //获取WebView的设置对象 WebSettings mWebSettings=myWebView.getSettings(); //如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript mWebSettings.setJavaScriptEnabled(true); mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true); //mWebSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小 //mWebSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 //缩放操作 mWebSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。 mWebSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false,则该WebView不可缩放 mWebSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件 //其他细节操作 mWebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存 mWebSettings.setAllowFileAccess(true); //设置可以访问文件 mWebSettings.setLoadsImagesAutomatically(true); //支持自动加载图片 mWebSettings.setDefaultTextEncodingName("utf-8");//设置编码格式 //所有JS弹框都不能直接显示 myWebView.setWebChromeClient(new WebChromeClient(){ @Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder b = new AlertDialog.Builder(WebViewActivity.this); b.setTitle("Alert"); b.setMessage(message); b.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); b.setCancelable(false); b.create().show(); return true; } }); } //android访问JS参考内容https://www.jianshu.com/p/345f4d8a5cfa @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ if(myWebView.canGoBack()) { myWebView.goBack();//回退 return true; } } return super.onKeyDown(keyCode, event); } @Override protected void onDestroy() { super.onDestroy(); if(myWebView!=null){ myWebView.clearHistory();//清楚历史记录 myWebView.destroy(); myWebView=null; } }
需要添加权限:
<uses-permission android:name="android.permission.INTERNET" />