将url从Activity1加载到Activity2,其中包含一个Web视图

本文关键字:包含一 视图 Web Activity2 url Activity1 加载 | 更新日期: 2023-09-27 17:58:06

如何将url从firstActivity加载到webpageActivity?我希望能够用firstActivity中的url点击一个按钮,然后将其传递到网页活动并加载url。

这是我的代码:FirstActivity

protected override void OnCreate(捆绑包){基础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);
}

}

将url从Activity1加载到Activity2,其中包含一个Web视图

通常使用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);

这有帮助吗?