Android 触发js的addEventListener自定义事件
Android 端:
private void triggerEvnet(String eventName, String detail){ if(eventName==null || eventName.length()==0){ return; } if(detail==null || detail.length()==0){ detail = "{}" } String js_string = "var myevent = new CustomEvent("+ eventName +", {detail: "+ detail +"});"; js_string += "window.dispatchEvent(myevent);"; webview.loadUrl("javascript:"+js_string); } // 例如:触发KeyboardShow事件,将键盘高度当作detail参数 triggerEvnet("KeyboardShow","{keyboardHeight:300}");
前端:
window.addEventListener("KeyboardShow",function(e){ console.log("Keyboard Height: " + e.detail.keyboardHeight) })