如何在打开指定的URL时关闭WebViewClient

时间:2013-09-30 09:16:31

标签: c# android android-webview xamarin

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

我覆盖 ShouldOverrideUrlLoading ,并在我的网页视图中加载新网址。 但在某些网址上,我只想关闭网页视图。

我尝试在 ShouldOverrideUrlLoading 中添加它,但我无法在那里开始/完成活动。我也尝试在网页类中创建一个函数,但是我没能从嵌套类中调用它。

现在我只是在按下后关闭webview,但我不希望用户做那么多工作..

我使用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();
    }
}

1 个答案:

答案 0 :(得分:2)

我建议将委托传递到用于关闭网络活动的HelloWebViewClient类,并将主要活动重新置于焦点。

要做到这一点:

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

 public delegate void OnLinkSelectedHandler(string url);

2:OnLinkSelectedHandler内创建WebPage的实施,该实施将关闭当前活动并重新关注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:ShouldOverrideUrlLoading中的HelloWebViewClient方法中实施调用委托的逻辑:

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));
    }
}