简体   繁体   中英

How to display alert function from xamarin android button in webview

  • How to display alert function from xamarin android button in webview
  • I want to add an alert in webview when the user clicks on the Xamarin android button.

在此处输入图像描述 Implement webviewclient object for your webview and call webview.EvaluateJavascript in OnPageFinished like this..

//create an internal webviewclient class
 internal class webViewClient : WebViewClient
    {
        public override void OnPageStarted(WebView view, string url, Bitmap favicon)
        {
            base.OnPageStarted(view, url, favicon);
        }

        public override void OnPageFinished(WebView view, string url)
        {
            base.OnPageFinished(view, url);
              //define the javascript code you wish to run
            string script = "javascript:alert('Hi');";
            //perform check and then call
            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
            {
                view.EvaluateJavascript(script, null);
            }
            else
            {
                view.LoadUrl(script);
            }
        }
    }

and then in your webview code in either OnCreate or in buttonclick evet handler do this

 protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.webview);
            mywebview = FindViewById<WebView>(Resource.Id.mywebiview);
            //This webviewchromeclient is very important
            mywebview.SetWebChromeClient(new WebChromeClient());
            WebSettings webSettings = mywebview.Settings;
            //set javascript enabled for your webview
            webSettings.JavaScriptEnabled = true;
            webSettings.JavaScriptCanOpenWindowsAutomatically = true;
             //set the object we defined above as the webviewclient for your webview
            mywebview.SetWebViewClient(new webViewClient());
         }

I have try this but not work =>webView.EvaluateJavascript("javascript: alert('Hi');", null);

You can try to use WebChromeClient() by webview1.SetWebChromeClient(new WebChromeClient());

  protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
      
        webview1 = FindViewById<WebView>(Resource.Id.webView1);
        button1 = FindViewById<Button>(Resource.Id.button1);

        button1.Click += Button1_Click;

        WebSettings settings = webview1.Settings;
        settings.JavaScriptEnabled = true;
        
        webview1.SetWebChromeClient(new WebChromeClient());        
        webview1.LoadUrl("file:///android_asset/login.html");
   
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {
        //webview1.EvaluateJavascript("javascript: check();", new EvaluateBack());
        webview1.EvaluateJavascript("javascript: alert('Hi');", null);
    }

The screenshot:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM