安卓webview调用code_mirror使用说明
在webview中可以使用code_mirror可以做成一个代码查看界面,在java代码中实现js调用的接口,调用后直接执行;
js代码如下,会自动生成一个html网页代码,自动插入到body中。
window.onload = function () { var name = SourceEditor.getName(); var extension = getExtension(name); if ("png" == extension || "gif" == extension) { loadImage(extension, SourceEditor.getRawContent()); return; } else if ("jpg" == extension || "jpeg" == extension) { loadImage("jpeg", SourceEditor.getRawContent()); return; } CodeMirror.modeURL = "code_mirror/mode/%N/%N.js"; var config = {}; config.value = SourceEditor.getContent(); config.readOnly = "nocursor"; config.lineNumbers = true; config.autofocus = false; config.lineWrapping = !!SourceEditor.getWrap(); config.dragDrop = false; config.fixedGutter = false; var editor = CodeMirror(document.body, config); var mode, spec; var info = CodeMirror.findModeByExtension(extension); if (info) { mode = info.mode; spec = info.mime; } if (mode) { editor.setOption("mode", spec); CodeMirror.autoLoadMode(editor, mode); } if (!config.lineWrapping) updateWidth(); };
所有js调用的java函数,需要用@JavascriptInterface进行注解,也可以把webview执行的结果返回给java程序