简体   繁体   English

如何在 Z5A98E2840FD0141780D854E48C608D 中显示来自 xamarin android 按钮的警报 function

[英]How to display alert function from xamarin android button in webview

  • How to display alert function from xamarin android button in webview如何在 Z5A98E2840FD0141780D854E48C608D 中显示来自 xamarin android 按钮的警报 function
  • I want to add an alert in webview when the user clicks on the Xamarin android button.当用户单击 Xamarin android 按钮时,我想在 webview 中添加警报。

在此处输入图像描述 Implement webviewclient object for your webview and call webview.EvaluateJavascript in OnPageFinished like this..为您的 webview 实施 webviewclient object 并像这样在OnPageFinished中调用webview.EvaluateJavascript

//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然后在OnCreatebuttonclick evet handler中的webview代码中执行此操作

 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);我试过了,但不行 =>webView.EvaluateJavascript("javascript: alert('Hi');", null);

You can try to use WebChromeClient() by webview1.SetWebChromeClient(new WebChromeClient());您可以尝试通过webview1.SetWebChromeClient(new WebChromeClient());使用 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:截图:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Xamarin android 中的警报弹出窗口显示警报弹出窗口 - How to show an Alert PopUp from an Alert PopUp in Xamarin android 如何在Android上的WebView上显示浮动按钮 - How to display floating button over webview on android 如何更改 xamarin.android 中默认显示警报的背景颜色? - How to change Background Color of Default Display alert in xamarin.android? Android Webview 警报对话框按钮 白色 - Android Webview Alert Dialog Button White colour 如何使用手机默认打印机功能在Xamarin Android中打印从WebView打开的PDF内容 - How to use phone default printer function to print the content of PDF opened from WebView in Xamarin Android 在Android Xamarin CrossWalk Webview中从Javascript调用C#函数 - Call C# Function From Javascript in Android Xamarin CrossWalk Webview Xamarin:Android WebView不显示本地PDF - Xamarin: Android WebView does not display local PDF 如何从WebView中的按钮启动相机-Android - How to launch camera from button in webview -Android 如何从Xamarin / Android Webview localstorage接收当前访问令牌? - How to receive current access token from Xamarin / Android webview localstorage? 如何从WebView的页面中获取数据? Android Xamarin - How to get data from the page in WebView? Android Xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM