前期需要知道的知識儲備
1彭雾,document.execCommand(sCommand[,交互方式,動態(tài)參數(shù)])
其中:sCommand為指令參數(shù)(如”2D-Position”),交互方式參數(shù)如果是true的話將顯示對話框锁保,如果為false的話,則不顯示對話框(下例中的”false”即表示不顯示對話框),動態(tài)參數(shù)一般為一可用值或?qū)傩灾担ㄈ缦吕械摹眛rue”)爽柒。
2吴菠,bold,italic,subscript,underline等等
3,html語句浩村,例如:
html = "<img
src="/i/eg_tulip.jpg"alt="上海鮮花港-郁金香"/>"
document.execCommand('insertHTML',false, html);
4, webView中交互監(jiān)聽(js代碼中)
this.wrappedObject.bind('tap', function(e) {thisObj.handleTapEvent(e); });
this.wrappedObject.bind('focus', function(e) {thisObj.handleFocusEvent(e); });
this.wrappedObject.bind('blur', function(e) {thisObj.handleBlurEvent(e); });
this.wrappedObject.bind('keydown', function(e) {thisObj.handleKeyDownEvent(e); });
this.wrappedObject.bind('input', function(e) { thisObj.handleInputEvent(e);});
Android原生代碼和js交互核心
1,原生代碼:
if(Build.VERSION.SDK_INT < 17) {
mWebView.setJsCallbackReceiver(newJsCallbackReceiver(this));
} else {
mWebView.addJavascriptInterface(newJsCallbackReceiver(this), JS_CALLBACK_HANDLER);
}
在WebViewClient的shouldOverrideUrlLoading方法中執(zhí)行mJsCallbackReceiver.executeCallback(callbackId, params)方法做葵。
注意callbackId是js回調(diào)過來的key和JsCallbackReceiver中定義的key是一樣的,用于區(qū)分不同js執(zhí)行事件
在JsCallbackReceiver中的js回傳中做自己處理
2,js中代碼:
window.nativeCallbackHandler.executeCallback("callback_image_click",imageNode.src);(就是動態(tài)語言心墅,參數(shù)動態(tài)化)
executeCallback為原生JsCallbackReceiver中的方法
注意點:
executeCallback方法上需要加@JavascriptInterfae//17之后的版本酿矢,必須加該注解,否則無法調(diào)用
3,webview需要先加載html文件怎燥,此處為本地文件
mWebView.loadDataWithBaseURL("file:///android_asset/",htmlEditor, "text/html","utf-8", "");
最后頁面樣式可以通過CSS樣式控制瘫筐,會寫js語句就更快搞定了!
編碼過程中遇到的坑
1铐姚,鍵盤換行符號失效
需要將編輯器設(shè)置為多行策肝,監(jiān)聽enter鍵盤,執(zhí)行document.execCommand("insertLineBreak");