java - 如何在 webview 中加载内部链接?

标签 java android

我在尝试让内部链接在我为小型企业所做的基于 webview 的 android 客户端中打开时遇到问题。我需要做什么才能让应用程序只能打开带有“https://forums.mywebsite.com”和“https://mywebsite.com”的链接?

我已经有了使用操作 View 打开外部链接所需的程序,但这仅链接到 loadurl() 中声明的第一页,并且不会在客户端内链接它们,并将它们视为外部链接。

aWebView.setWebViewClient(new WebViewClient() {
                                          @Override
                                          @TargetApi(21)
                                          public boolean shouldOverrideUrlLoading(WebView aWebView, WebResourceRequest request) {
                                              Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());

                                              superWebView.getContext().startActivity(intent);

                                              return true;
                                          }
                                      });

TL;DR:链接预期不起作用,我们将不胜感激。

最佳答案

作为docs解释:

returning true causes the current WebView to abort loading the URL, while returning false causes the WebView to continue loading the URL as usual.

您应该在检查 URL 的方法中包含逻辑,仅当 url 与您预期的 URL 匹配时才返回 false 并启动 Activity ,否则返回 true。

类似的东西:

    WebView.setWebViewClient(new WebViewClient() {
        @Override
        @TargetApi(21)
        public boolean shouldOverrideUrlLoading(WebView aWebView, WebResourceRequest request) {
            if (TextUtils.equals(request.getUrl().toString(), "https://forums.mywebsite.com")
                    ||TextUtils.equals(request.getUrl().toString(), "https://mywebsite.com")) {
                return false;
            } else {
                Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
                superWebView.getContext().startActivity(intent);
                return true;
            }
        }
    });

关于java - 如何在 webview 中加载内部链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55325439/

相关文章:

java - 运行 nokia 6212 模拟器时出现内部 RMI 注册表端口问题

java - LinearLayout 上的监听器

android - 在android中根据用户当前设备​​位置生成推送通知

android - Android 动画中的 XScale 与 XDelta

java - 如何从另一个包访问 protected 变量

Java:编写一个抛出任何 RuntimeException 的通用方法

java - 我可以将 Spring 框架与 swing/SET 一起使用吗?

java - 字符串转十六进制短java

java - 如何使用for循环一次运行多个登录?

java - 如何根据客户端的分辨率调整应用程序组件的大小?