如何使用MonoDroid打开网站?

时间:2012-01-09 21:41:35

标签: android xamarin.android

如何使用Mono for Android打开网站?我假设我需要使用Intent,但我不知道哪一个。

2 个答案:

答案 0 :(得分:4)

var intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse("http://www.stackoverflow.com"));

StartActivity(intent);

答案 1 :(得分:2)

另一种可能性是创建WebView并在其中加载URL,这样您就可以更好地控制它的外观以及它对Javascript等内容的反应。

您可以像这样创建自己的活动:

using System;

using Android.App;
using Android.OS;
using Android.Webkit;
using Android.Views;

namespace WebViewSample
{
    [Activity(Label = "MyAwesomeWebActivity", MainLauncher = true, Icon = "@drawable/icon")]
    public class MyAwesomeWebActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            WebView webView = new WebView(this);
            webView.Settings.JavaScriptEnabled = true;
            webView.Settings.SetSupportZoom(true);
            webView.Settings.BuiltInZoomControls = true;
            webView.Settings.LoadWithOverviewMode = true; //Load 100% zoomed out
            webView.ScrollBarStyle = ScrollbarStyles.OutsideOverlay;
            webView.ScrollbarFadingEnabled = true;


            webView.VerticalScrollBarEnabled = true;
            webView.HorizontalScrollBarEnabled = true;

            webView.SetWebViewClient(new AwesomeWebClient());
            webView.SetWebChromeClient(new AwesomeWebChromeClient(this));

            AddContentView(webView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent));

            webView.LoadUrl("http://stackoverflow.com");
        }

        private class AwesomeWebClient : WebViewClient { }

        private class AwesomeWebChromeClient : WebChromeClient
        {
            private Activity mParentActivity;
            private string mTitle;

            public AwesomeWebChromeClient(Activity parentActivity)
            {
                mParentActivity = parentActivity;
                mTitle = parentActivity.Title;
            }

            public override void OnProgressChanged(WebView view, int newProgress)
            {
                mParentActivity.Title = string.Format("Loading {0}%", newProgress);
                mParentActivity.SetProgress(newProgress * 100);

                if (newProgress == 100) mParentActivity.Title = mTitle;
            }
        }
    }
}

你在这里有很多可能性。