Ejemplo n.º 1
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

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

            #region WebView

            var client = new ContentWebViewClient();

            client.WebViewLocaitonChanged += (object sender, ContentWebViewClient.WebViewLocaitonChangedEventArgs e) => {
                Debug.WriteLine(e.CommandString);
            };

            client.WebViewLoadCompleted += (object sender, ContentWebViewClient.WebViewLoadCompletedEventArgs e) => {
                RunOnUiThread(() => {
                    AndHUD.Shared.Dismiss(this);
                });
            };


            MyWebView = FindViewById <WebView> (Resource.Id.web_mywebview_webview);
            // NOTICE : 先換成一般的 WebViewClient
            MyWebView.SetWebViewClient(client);
            //MyWebView.SetWebViewClient(new MyWebClient());
            MyWebView.Settings.JavaScriptEnabled = true;
            MyWebView.Settings.UserAgentString   = @"Android";

            // 負責與頁面溝通 - WebView -> Native
            MyJSInterface myJSInterface = new MyJSInterface(this);

            MyWebView.AddJavascriptInterface(myJSInterface, "TP");
            myJSInterface.CallFromPageReceived += delegate(object sender, MyJSInterface.CallFromPageReceivedEventArgs e) {
                Debug.WriteLine(e.Result);
            };

            // 負責與頁面溝通 - Native -> WebView
            JavaScriptResult callResult = new JavaScriptResult();
            callResult.JavaScriptResultReceived += (object sender, JavaScriptResult.JavaScriptResultReceivedEventArgs e) => {
                Debug.WriteLine(e.Result);
            };


            // 載入一般網頁
            //MyWebView.LoadUrl ("http://stackoverflow.com/");
            // 載入以下程式碼進行互動

            /*
             * MyWebView.LoadDataWithBaseURL (
             *      null
             *      , @"<html>
             *                      <head>
             *                              <title>Local String</title>
             *                              <style type='text/css'>p{font-family : Verdana; color : purple }</style>
             *                              <script language='JavaScript'>
             *                                      var lookup = '中文訊息'
             *                                      function msg(){ window.location = 'callfrompage://Hi'  }
             *                              </script>
             *                      </head>
             *                      <body><p>Hello World!</p><br />
             *                              <button type='button' onclick='TP.CallFromPage(lookup)' text='Hi From Page'>Hi From Page</button>
             *                      </body>
             *              </html>"
             *      , "text/html"
             *      , "utf-8"
             *      , null);
             *
             */

            #endregion

            #region EditText

            _InputMethodManager =
                (InputMethodManager)GetSystemService(Context.InputMethodService);



            TxtUrl = FindViewById <EditText> (Resource.Id.web_mywebview_txtUrl);

            TxtUrl.TextChanged += (object sender,
                                   Android.Text.TextChangedEventArgs e) => {
                Debug.WriteLine(TxtUrl.Text + ":" + e.Text);
            };

            #endregion


            BtnGo        = FindViewById <Button> (Resource.Id.web_mywebview_btnGo);
            BtnGo.Click += (object sender, EventArgs e) => {
                //RunOnUiThread (() => {
                //	MyWebView.EvaluateJavascript (@"msg();", callResult);
                //});


                var url = TxtUrl.Text.Trim();

                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.SetTitle(url);
                alert.SetMessage("");
                alert.SetNegativeButton("取消", (senderAlert, args) => {
                });
                alert.SetPositiveButton("確認", (senderAlert, args) => {
                    RunOnUiThread(
                        () => {
                        AndHUD.Shared.Show(this, "Status Message", -1, MaskType.Clear);
                    }

                        );

                    MyWebView.LoadUrl(url);
                });

                RunOnUiThread(() => {
                    alert.Show();
                });

                //
                _InputMethodManager.HideSoftInputFromWindow(
                    TxtUrl.WindowToken,
                    HideSoftInputFlags.None);
            };
        }