【问题标题】:Load url from Activity1 to Activity2 which contains a webview将 Activity1 的 url 加载到包含 webview 的 Activity2
【发布时间】:2014-04-12 17:00:00
【问题描述】:

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

这是我的代码:FirstActivity

protected override void OnCreate(捆绑包) { base.OnCreate(捆绑);

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

}

【问题讨论】:

    标签: c# android url webview xamarin


    【解决方案1】:

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

    这有帮助吗?

    【讨论】:

    • 我的下一个问题是如何覆盖它,以便如果单击另一个按钮,比如我从第一个活动添加另一个 url,我希望它加载不同的 url,但在同一个网页活动中?例如'var intent2 = new Intent (this, typeof(WebPage)); intent.PutExtra("url2", "google.com");开始活动(intent2);'那么第二个活动我该怎么做呢?
    • 我让它工作了。非常感谢您的帮助:D
    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多