protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // SetContentView(Resource.Layout.Main); // var client = new ContentWebViewClient(); // MyWebView = FindViewById<WebView>(Resource.Id.webview); MyWebView.SetWebViewClient(client); MyWebView.Settings.JavaScriptEnabled = true; // 負責與頁面溝通 - WebView -> Native MyJSInterface myJSInterface = new MyJSInterface(this); MyWebView.AddJavascriptInterface(myJSInterface, "TP"); myJSInterface.CallFromPageReceived += delegate (object sender, MyJSInterface.CallFromPageReceivedEventArgs e) { WriteLine(e.Result); AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.SetTitle("Info"); alert.SetMessage($"回傳內容 { e.Result }"); alert.SetPositiveButton("確認", (senderAlert, args) => { }); RunOnUiThread(() => { alert.Show(); }); }; 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); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // SetContentView(Resource.Layout.Main); // TxtUrl = FindViewById<EditText>(Resource.Id.txtUrl); _InputMethodManager = (InputMethodManager)GetSystemService(Context.InputMethodService); _InputMethodManager.HideSoftInputFromWindow( TxtUrl.WindowToken, HideSoftInputFlags.None); // var client = new ContentWebViewClient(); client.WebViewLocaitonChanged += (sender, e) => { WriteLine($"{ e.CommandString }"); }; // MyWebView = FindViewById<WebView>(Resource.Id.webview); MyWebView.SetWebViewClient(client); MyWebView.Settings.JavaScriptEnabled = true; // 負責與頁面溝通 - Native -> WebView JavaScriptResult callResult = new JavaScriptResult(); callResult.JavaScriptResultReceived += (object sender, JavaScriptResult.JavaScriptResultReceivedEventArgs e) => { WriteLine(e.Result); RunOnUiThread(() => { TxtUrl.Text = e.Result; }); }; // BtnGo = FindViewById<Button>(Resource.Id.btnGo); BtnGo.Click += (sender, e) => { RunOnUiThread(() => { MyWebView.EvaluateJavascript(@"msg( 1234 );", callResult); }); }; 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( text ){ return text + ' received'; } </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); }
protected override void OnCreate (Bundle savedInstanceState) { base.OnCreate (savedInstanceState); // SetContentView (Resource.Layout.Main); var client = new ContentWebViewClient (); client.WebViewLocaitonChanged += (object sender, ContentWebViewClient.WebViewLocaitonChangedEventArgs e) => { WriteLine (e.CommandString); }; client.WebViewLoadCompleted += (object sender, ContentWebViewClient.WebViewLoadCompletedEventArgs e) => { RunOnUiThread (() => { AndHUD.Shared.Dismiss (this); }); }; MyWebView = FindViewById<WebView> (Resource.Id.webview); MyWebView.SetWebViewClient (client); MyWebView.Settings.JavaScriptEnabled = true; MyWebView.Settings.UserAgentString = @"Android"; #region EditText _InputMethodManager = (InputMethodManager)GetSystemService (Context.InputMethodService); TxtUrl = FindViewById<EditText> (Resource.Id.txtUrl); TxtUrl.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => { WriteLine (TxtUrl.Text + ":" + e.Text); }; #endregion BtnGo = FindViewById<Button> (Resource.Id.btnGo); BtnGo.Click += (object sender, EventArgs e) => { // 隱藏鍵盤 _InputMethodManager.HideSoftInputFromWindow ( TxtUrl.WindowToken, HideSoftInputFlags.None); // var url = TxtUrl.Text.Trim (); // 詢問是否要過去指定頁面 AlertDialog.Builder alert = new AlertDialog.Builder (this); alert.SetTitle ("Info"); alert.SetMessage ( $"請問是否前往 { url }" ); alert.SetNegativeButton ("取消", (senderAlert, args) => { }); alert.SetPositiveButton ("確認", (senderAlert, args) => { RunOnUiThread ( () => { AndHUD.Shared.Show (this, "Status Message", -1, MaskType.Clear); } ); MyWebView.LoadUrl (url); }); RunOnUiThread (() => { alert.Show (); }); }; }