2

现在我在网页类中有一个嵌套的 webviewclient 类。

我覆盖了ShouldOverrideUrlLoading,并在我的 webview 中加载了新的 url。但在某些网址上,我只想关闭 webview。

我尝试在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();
    }
}
4

1 回答 1

3

我建议将委托传递给HelloWebViewClient用于关闭网络活动并使主要活动重新成为焦点的类。

去做这个:

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

 public delegate void OnLinkSelectedHandler(string url);

2:创建 inside 的实现OnLinkSelectedHandlerWebPage将关闭当前活动并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.SingleTop并将ActivityFlags.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));
    }
}
于 2013-10-01T09:33:54.513 回答