安卓:如何将本地javascript文件加载到WebView?

3
我正在尝试编写一个简单的“toast”程序。当我试图将脚本添加到HTML中时,它可以工作并且我能够看到“toast”。但是,当我将相同的脚本代码放在assert文件夹(script.js)下的单独文件中时,它无法注入JavaScript。
我支持API 15及以上版本。
以下是我尝试注入脚本的代码。
    myWebView = (WebView) findViewById(R.id.webviewid);

    myWebView.loadDataWithBaseURL("file:///android_asset/",
            "<html>" +
            "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">" +
            "<style type=\"text/css\">\n" +
            "@font-face {\n" +
            "    font-family: MyFont;\n" +
            "    src: url(\"file:///android_asset/fonts/myfont.ttf\")\n" +
            "}\n" +
            "body {\n" +
            "    font-family: MyFont;\n" +
            "    font-size: medium;\n" +
            "    text-align: justify;\n" +
            "}\n" +
            "</style>" +
            "</head>" +
            "<body>" +
            "<input type=\"button\" value=\"Say hello\" onClick=\"showAndroidToast('Hello Android!')\" />" +
            "<script type=\"text/javascript\" src=\"file:///android_asset/www/js/script.js\"/>" +
            "</body>" +
            "</html>", "text/html", "UTF-8", null);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
    myWebView.setWebViewClient(new MyWebViewClient());

这是我的 javascript 代码(script.js)。

    function showAndroidToast(toast) {
        Android.showToast(toast);
    };

这里是我的JavaScript接口:
    protected class WebAppInterface {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c) {
            mContext = c;
        }

        /** Show a toast from the web page */
        @JavascriptInterface
        public void showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }

如何注入javascript文件?感谢您的帮助。

我的项目结构如下:

    ---
      |
      ---- App
           |
           -----src
                |
                -----main
                     |
                     ------asserts
                     |     |
                     |     ------fonts
                     |     |
                     |     ------www
                     |     |     |
                     |     |     ------js
                     |     |           |
                     |     |           ------script.js
                     |
                     ------java
                     |
                     ------res

猫日志显示:
 E/Web Console﹕ Uncaught ReferenceError: showAndroidToast is not defined:13
1个回答

0

由于您正在使用WebView来显示数据,因此您可以将其上传到服务器甚至Dropbox,然后通过URL导入进行连接?


你是在检查系统是否能够获取脚本并且不会返回空白或者无法在源文件夹中找到它吗? - Dogy Vogey
那就是,它没有加载脚本。 - User12111111
尝试将它放入你的“字体”文件夹中,并像你所做的字体样式文件一样进行链接,如果它能正常工作,那么你就知道它没有正确找到你的目录。 - Dogy Vogey
好的,你能和我分享任何可用的工作示例吗?那会帮助我进一步开展工作。 - User12111111
我收到了以下错误: E/Web Console﹕ Uncaught ReferenceError: showAndroidToast is not defined:13 - User12111111
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接