如何在打开指定的 URL 时关闭 Web 视图客户端

本文关键字:Web 客户端 视图 URL | 更新日期: 2023-09-27 18:33:14

现在我在网页类中得到了一个嵌套的webviewclient类。

我覆盖了ShouldOverrideUrlLoad,并使新的url加载到我的Web视图中。但是在某些网址上,我只想关闭网络视图。

我尝试将其添加到应该覆盖网址加载中,但我无法在那里开始/完成活动。我还尝试在网页类中创建一个函数,但我未能从嵌套类调用它。

现在我只是在按下后退时关闭 Web 视图,但我不希望用户必须做那么多工作。

我使用 Xamarin(C#) 为 Android 开发,但 Java 的答案很可能也会有所帮助!

[Activity (Label = "WebPage", Theme = "@android:style/Theme.NoTitleBar")]
public class WebPage : Activity
{
    WebView web_view;
    private class HelloWebViewClient : WebViewClient
    {
        public override bool ShouldOverrideUrlLoading (WebView view, string url)
        {
            view.LoadUrl (url);
            return true;
        }
    }
    public void dofinish()
    {
        var activity2 = new Intent (this, typeof(MainActivity));
        activity2.PutExtra("targeturl", targeturl);
        StartActivity(activity2);
        Finish();
    }
    public string targeturl;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        string text = Intent.GetStringExtra ("MyData") ?? "Data not available";
        targeturl = Intent.GetStringExtra ("targeturl") ?? "No Target Url";
        SetContentView (Resource.Layout.WebView);
        web_view = FindViewById<WebView> (Resource.Id.LocalWebview);
        web_view.Settings.JavaScriptEnabled = true;
        web_view.LoadUrl (text);
        web_view.SetWebViewClient (new HelloWebViewClient ());
    }
    public override void OnBackPressed()
    {
        var activity2 = new Intent (this, typeof(MainActivity));
        activity2.PutExtra("targeturl", targeturl);
        StartActivity(activity2);
        Finish();
    }
}

如何在打开指定的 URL 时关闭 Web 视图客户端

我建议将委托传递到用于关闭 Web 活动并使主要活动重新聚焦的 HelloWebViewClient 类中。

为此:

1:声明将用于关闭 Web 视图活动的委托类型:

 public delegate void OnLinkSelectedHandler(string url);

2:WebPage中创建OnLinkSelectedHandler的实现,以关闭当前活动并使MainActivity重新成为焦点:

public void dofinish(string url)
{
    // Bring the other activity into focus.
    var activity2 = new Intent (this, typeof(MainActivity));
    activity2.AddFlags (ActivityFlags.SingleTop | ActivityFlags.ClearTop);
    activity2.PutExtra("targeturl", url);
    StartActivity(activity2);
    // Close this activity.
    Finish();
}

ActivityFlags.SingleTopActivityFlags.ClearTop添加其他标志将导致目标活动重新成为焦点,而无需在活动堆栈上创建它的新实例。

3:HelloWebViewClient中实现在ShouldOverrideUrlLoading方法中调用委托的逻辑:

public override bool ShouldOverrideUrlLoading (WebView view, string url)
{
    view.LoadUrl (url);
    if (url == "http://stackoverflow.com/about")
    {
        this.linkSelected (url);
    }
    return true;
}

当这一切汇集在一起时:

[Activity (Label = "WebPage", Theme = "@android:style/Theme.NoTitleBar")]
public class WebPage : Activity
{
    WebView web_view;
    public delegate void OnLinkSelectedHandler (string url);
    private class HelloWebViewClient : WebViewClient
    {
        private OnLinkSelectedHandler linkSelected;
        public HelloWebViewClient(OnLinkSelectedHandler handler)
        {
            linkSelected = handler;
        }
        public override bool ShouldOverrideUrlLoading (WebView view, string url)
        {
            view.LoadUrl (url);
            if (url == "http://stackoverflow.com/about")
            {
                this.linkSelected (url);
            }
            return true;
        }
    }
    public void dofinish(string url)
    {
        // Bring the other activity into focus.
        var activity2 = new Intent (this, typeof(MainActivity));
        activity2.AddFlags (ActivityFlags.SingleTop | ActivityFlags.ClearTop);
        activity2.PutExtra("targeturl", url);
        StartActivity(activity2);
        // Close this activity.
        Finish();
    }
    public string targeturl;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.WebViewTest);
        web_view = FindViewById<WebView> (Resource.Id.webView1);
        web_view.LoadUrl ("http://stackoverflow.com");
        // Pass the callback used to close this activity.
        web_view.SetWebViewClient (new HelloWebViewClient (this.dofinish));
    }
}