Android:活动内的 Web 视图无法正确运行本地存储的 Web 应用程序

发布于 2025-01-01 09:34:47 字数 1805 浏览 4 评论 0原文

我正在处理以下问题:

我在活动中有一个网络视图:

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);

webView.setWebViewClient(new HelloWebViewClient());
System.out.println("percorso" + path.toString());
url.setText(path.toString()); 
webView.loadUrl("local path of my html file" .....\..\..page.htm);

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

这是我想在本地离线运行的应用程序:http://miniapps.co.uk/checklist/ 如果我打开浏览器并转到上面的链接,一切正常,因此 Android 浏览器实际上能够运行它。

现在我已经下载了这个应用程序并将其存储在我的 SD 卡上的文件夹中:download/myapp 在文件夹中,我有 html 文件:check_list.htm 和一个文件夹(check_list 文件),其中包含运行它所需的 javascript 文件。

但是当我在网络视图中打开它时,页面已正确加载但不起作用...我无法单击按钮和其他元素,我实际上无法与它交互...

我认为这可能是与我的 webview 设置相关的问题,所以我使用 astro 文件管理器浏览我的 SD 卡,我单击了 check_list.htm,我选择了 html 查看器,我遇到了同样的问题....

所以我下载了 Opera Mobile,然后我做了一样,不过这次我用 opera 打开它现在它工作正常了。

你知道为什么吗?我该如何解决这个问题?我是否缺少 webview 上的某些设置,或者 webview\android broser 在打开本地存储的 html 页面和运行链接的 javascript 文件时出现一些问题?

编辑 如果我打开 Android 浏览器并写入 HTML 文件的路径,它就可以工作。我必须手动执行此操作,因为如果我单击 HTML 文件,Android 浏览器就无法选择......所以最终它可以在 Android 浏览器和 Opera 上运行,并且不适用于 HTML 查看器和我的浏览器网页视图......所以我缺少一些设置,因为网页视图应该具有与Android浏览器相同的功能......不是吗?

I'm dealing with the following problem:

I have a webview inside an activity:

webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setBuiltInZoomControls(false);
webView.getSettings().setSupportZoom(false);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);

webView.setWebViewClient(new HelloWebViewClient());
System.out.println("percorso" + path.toString());
url.setText(path.toString()); 
webView.loadUrl("local path of my html file" .....\..\..page.htm);

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

This is the app I'd like to run locally offline: http://miniapps.co.uk/checklist/
If I open the browser and i go to the link above everything works fine, so the android browser is actually able to run it.

Now I have downloaded this app and I stored it in a folder on my sd card: download/myapp
in the folder I have the html file: check_list.htm and a folder (check_list files) with the javascript files needed to run it.

But when i open it inside my webview, the page is correcty loaded but doesn't work...i can't click on buttons and other elements, i can't actually interact with it....

I thought it could be a problem related to my webview settings so i used astro file manager to surf into my sd card, i clicked on check_list.htm, i chose html viewer and i got the same issues....

So I downloaded opera mobile, and i did the same but this time I opened it with opera and now it's working correctly.

Do you know why? How can I fix the problem? Am I missing some settings on my webview or is the webview\android broser that hase some problems opening locally stored html pages and running linked javascript files?

EDIT
If I open the android browser and I write the path of the HTML file, it works. I have to do it manually cause if I click on the HTML file, android browser it's not an option......so in the end it works both on android browser and opera, and it doesn't with HTML viewer and my web view.....so I'm missing some settings cause the web view should have the same features of the android browser....shouldn't it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请持续率性 2025-01-08 09:34:47

如果“不运行”意味着它不打开新窗口,那是因为你需要指定 webview 客户端。

来自 WebView 的 Javadoc

默认情况下,HTML 发出的打开新窗口的请求将被忽略。无论它们是通过 JavaScript 还是通过链接上的目标属性打开,都是如此。您可以自定义 WebChromeClient 以提供您自己的打开多个窗口的行为,并以您想要的任何方式呈现它们。

请参阅 https://stackoverflow.com/a/3847016/94363 或 rtfm...

If "doesn't run" mean it does not open new windows, that's because you need to specify the webview client.

From the Javadoc for WebView

By default, requests by the HTML to open new windows are ignored. This is true whether they be opened by JavaScript or by the target attribute on a link. You can customize your WebChromeClient to provide your own behaviour for opening multiple windows, and render them in whatever manner you want.

See https://stackoverflow.com/a/3847016/94363 or rtfm...

呢古 2025-01-08 09:34:47

请先确认,您的本地文件位于 asset/www/index.html 目录中。

Please confirm before, your local file is inside asset/www/index.html directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文