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)
})

  

 

posted @ 2020-01-07 11:01  前端大兵  阅读(1835)  评论(0编辑  收藏  举报