gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:16:06 25 4
gpt4 key购买 nike

我在尝试让内部链接在我为小型企业所做的基于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com