将URL从Activity1加载到包含webview的Activity2

时间:2014-04-12 17:00:01

标签: c# android url webview xamarin

如何将firstActivity的网址加载到网页活动? 我希望能够点击带有来自firstActivity的网址的按钮,然后将其传递给网页活动并加载网址。

这是我的代码:FirstActivity

protected override void OnCreate(Bundle bundle)         {             base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it


        var scisnews = FindViewById<Button> (Resource.Id.scisnewsbtn);
        string scisnewsurl = "http://cis.ulster.ac.uk/news-a-events-mainmenu-70";


        //labinduction.Click += (sender, e) => {
        //  var LabInductionI = new Intent (this, typeof(LabInduction));
        //  StartActivity (LabInductionI);
        //};

        scisnews.Click += delegate {
            var ScisNewsI = new Intent (this, typeof(WebPage));
            ScisNewsI.PutExtra ("scisnews", scisnewsurl);
            this.StartActivity (ScisNewsI);
        };
    }

        public class HelloWebViewClient : WebViewClient
        {
            public override bool ShouldOverrideUrlLoading (WebView view, string url)
            {
            view.LoadUrl ("http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
                return true;
            }
        }

    }

代码:WebPageActivity

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Create your application here
        SetContentView (Resource.Layout.WebPageLO);

        web_view = FindViewById<WebView> (Resource.Id.webview);

        web_view.Settings.JavaScriptEnabled = true;
        web_view.Settings.BuiltInZoomControls = true;




        web_view.SetWebViewClient (new HelloWebViewClient ());



    }
}


public class HelloWebViewClient : WebViewClient
{
    public override bool ShouldOverrideUrlLoading (WebView view, string url)
    {
        view.LoadUrl ("http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
        return true;
    }
}


public override bool OnKeyDown (Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
    if (keyCode == Keycode.Back && web_view.CanGoBack ()) 
    {
        web_view.GoBack ();
        return true;
    }

    return base.OnKeyDown (keyCode, e);
}

}

1 个答案:

答案 0 :(得分:0)

您通常使用Intent类将信息从一个活动传递到另一个活动。它看起来像你正在尝试,但没有代码拉出价值。

以下是一个例子:

//In your first activity
var intent = new Intent(this, typeof(WebPage));
intent.PutExtra("url", "http://cis.ulster.ac.uk/news-a-events-mainmenu-70");
StartActivity(intent);

//Then in the second activity
string url = Intent.GetStringExtra("url");

//Then you can load the page like this
web_view.LoadUrl(url);

这有帮助吗?