2017年6月5日 星期一

Xamarin Android C# WebView 實現 Alert Confirm 事件


一、 在 OnCreate 增加以下項目:



//允許使用 javascript
webView.Settings.JavaScriptEnabled = true; 
webView.SetWebChromeClient(new CustChromeWebViewClient(this));

二、Override alert confirm 事件:

        private class CustChromeWebViewClient : WebChromeClient
        {
            private Context _context;

            public CustChromeWebViewClient(Context father)
            {
                _context = father;

            }

            public override bool OnJsAlert(WebView view, string url, string message, JsResult result)
            {
                new AlertDialog.Builder(_context).SetTitle("提示訊息").SetMessage(message).SetPositiveButton("確定", delegate
                {
                    result.Confirm();
                }).Create().Show();


                return true;

            }

            public override bool OnJsConfirm(WebView view, string url, string message, JsResult result)
            {

                new AlertDialog.Builder(_context).SetTitle("提示訊息").SetMessage(message).SetPositiveButton("確定", delegate
                {
                    result.Confirm();
                }).SetNegativeButton("取消",delegate {
                    result.Cancel();
                })
                .Create().Show();


                return true;

            }

        }

三、 完成以上項目後,就可以測試WebView 上響應 Alert Confirm 事件

3 則留言:

  1. 作者已經移除這則留言。

    回覆刪除
  2. Queen Casino Slots: Play Free Slots Online - Konicasino
    Queen Casino is a licensed online casino offering some 우리카지노 마틴 of the finest free slots and other casino sbobet ทางเข้า games. Play over 100 クイーンカジノ free games and join today!

    回覆刪除
  3. Casino Game For Sale by Hoyle - Filmfile Europe
    › casino-games 토토 총판 › casino-games real air jordan 18 retro yellow › casino-games › casino-games Casino Game for sale by Hoyle on Filmfile Europe. Free shipping 넷마블 토토 넷마블 for most air jordan 18 retro racer blue to good site countries, no download required. Check how can i get jordan 18 white royal blue the deals we have.

    回覆刪除