gpt4 book ai didi

android - 在 Android WebView 类中控制导航

转载 作者:行者123 更新时间:2023-11-30 02:05:22 25 4
gpt4 key购买 nike

我使用 WebView 类在我的 apk 中显示教程。我喜欢它,因为我可以在不对 apk 进行任何更改的情况下管理和更新教程。我将所有教程 html 文件保存在我们网站上的特定文件夹中。

我的问题是是否有任何事件可以让我捕捉开始导航(当用户单击链接时)。我想看看用户在我的教程中点击了哪个链接,只要用户在我们网站上的教程文件夹中导航,他就应该在我的 apk 的 WebView 中导航。但是,如果有任何链接指向教程文件夹之外,则应阻止导航到该链接并将导航传递给浏览器。

我不确定我是否解释清楚了。我对 Android 开发很陌生。请在下面查看我的 Windows 手机 C# 代码示例,我需要在 Android apk 中使用相同的代码

    /// <summary>
/// navigation in the application's browser goes as long as it is in the "stockscreenerapp" folder
/// if user goes out of this folder ("stockscreenerapp" cannot be found in the URL string) then
/// external default browser is called for further navigation
/// </summary>
private async void webBrowser1_NavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args) {
if (null != args.Uri) {
string url = args.Uri.ToString();
int i = url.IndexOf("stockscreenerapp"); /* check whether "stockscreenerapp" is in the URL address */
if (i == -1) { args.Cancel = true; await Windows.System.Launcher.LaunchUriAsync(args.Uri); /*if it is not we call external browser and pass URL to it*/}
else { imgLoading.Visibility = Visibility.Visible; /* show loading image while page is loading */ }
}
}

最佳答案

尝试使用 WebViewClient 并监听 onPageStarted .这样你就可以获得点击链接的 url,检查它是否在你的教程之外,并告诉 WebView stopLoading如果是的话。

在代码中,它可能看起来像这样(警告,未经测试):

WebView webView = new WebView(context);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted (WebView view, String url, Bitmap favicon) {
if(Tutorial.doesNotContain(url))
view.stopLoading();
view.goBack();
}
});

关于android - 在 Android WebView 类中控制导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30722138/

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