提到WebView很多同學可能并不陌生罕袋;Mobile端贫悄、PC端都有機會碰到這個容器,下面我們先來看看各自的定義:
Mobile 端以Android為例:Android WebView在Android平臺上是一個特殊的View峰鄙, 它內(nèi)部實現(xiàn)是采用渲染引擎來展示view的內(nèi)容辉巡,提供網(wǎng)頁前進后退,網(wǎng)頁放大与境,縮小验夯,搜索等功能。
PC端以JavaFX為例:JavaFX提供了一個GUI WebView(javafx.scene.web.WebView)節(jié)點摔刁,以將HTML5內(nèi)容呈現(xiàn)到場景圖形上挥转。WebView節(jié)點是一個小型瀏覽器。同樣內(nèi)部實現(xiàn)時采用渲染引擎來展示view的內(nèi)容共屈。
WebView的基本使用
1.? 不論Mobile還是PC,使用WebView時都需先new一個對象绑谣,如下:WebView?webview?=?new?WebView(this);?setContentView(webview); ---Mobile
WebView?webview?=?new?WebView();?WebEngine?engine = webView.getEngine();? ---PC
2.? 緊接著可以加載一個網(wǎng)頁,如下:? ?WebView?webview.loadUrl("http://developer.android.com/"); ---Mobile
<uses-permission android:name="android.permission.INTERNET"> ----Mobile
engine.setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"); ---PC
engine.load(defaultURL); ---PC
3.? 設置監(jiān)聽拗引,對窗體內(nèi)部元素進行監(jiān)控借宵,如下:public?class?TestFindListener?implements?android.webkit.WebView.FindListener?{? }; ---Mobile
public ChangeListener(JSHandler jHandler){}---PC, 小編本地具體實現(xiàn)方法?
4.? 具體操作( 根據(jù)各自需求來寫)
WebView給我們帶來了什么
首先,WebView可以將原生態(tài)App和Application中的某些功能結構抽離出來矾削,以加載HTML5的方式進行集成壤玫,于是功能結構可以并行開發(fā),提高了研發(fā)效率哼凯;
其次欲间,WebView提供了的靈活性,在應用升級時只需部署后臺服務器断部,前端框架無需大改猎贴;
然后,WebView加載的H5可以有很完美的css美化,是原生態(tài)App和Application比較難做到的她渴;
缺點:加載確實稍慢一籌达址,因為需要拼接還需要加載樣式,這里面有一定時間的消耗
? WebView在Android端的應用(源碼):
package com.example.webviewdemo;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Message;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewBase extends WebView {
private static final String DEFAULT_URL = "http://www.ijinshan.com/";
private Activity mActivity;
public WebViewBase(Context context) {
super(context);
mActivity = (Activity) context;
init(context);
}
@SuppressLint("SetJavaScriptEnabled")
private void init(Context context) {
WebSettings webSettings = this.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
//webSettings.setUseWideViewPort(true);
this.setWebViewClient(mWebViewClientBase);
this.setWebChromeClient(mWebChromeClientBase);
this.loadUrl(DEFAULT_URL);
this.onResume();
}
private WebViewClientBase mWebViewClientBase = new WebViewClientBase();
private class WebViewClientBase extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void doUpdateVisitedHistory(WebView view, String url,
boolean isReload) {
// TODO Auto-generated method stub
super.doUpdateVisitedHistory(view, url, isReload);
}
}
private WebChromeClientBase mWebChromeClientBase = new WebChromeClientBase();
private class WebChromeClientBase extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
mActivity.setProgress(newProgress * 1000);
}
@Override
public void onReceivedTitle(WebView view, String title) {
// TODO Auto-generated method stub
super.onReceivedTitle(view, title);
}
@Override
public void onReceivedTouchIconUrl(WebView view, String url,
boolean precomposed) {
// TODO Auto-generated method stub
super.onReceivedTouchIconUrl(view, url, precomposed);
}
@Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
// TODO Auto-generated method stub
return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
}
}
}
PC端暫時申請專利中惹骂,實現(xiàn)案例以后加載苏携!謝謝交流~!