Android WebView JS alert handling __c#

Source: Internet
Author: User

Beginning to write WebView students may have encountered such a problem, when your H5 page contains JS alert,confrim,prompt and other window, the runtime of these windows are invalid. Don't worry. Android has set aside the interface for us to rewrite these windows.

First we need to define a webview this comparison base here is not much to say, and then we need to call the Setwebviewclient method and set JavaScript to take effect.

<span style= "White-space:pre" >		</span>webview.setwebviewclient (New Webviewclient () {

			@ Override Public
			Boolean shouldoverrideurlloading (webview view, String URL) {
				view.loadurl (URL);
				View.getsettings (). Setjavascriptenabled (true);
				View.getsettings (). Setjavascriptcanopenwindowsautomatically (true);
				return true;
The point is, we need to rewrite Alert,confrim, prompt window.
<span style= "White-space:pre" > </span>webview.setwebchromeclient (New Webchromeclient () {@Override Pub  Lic Boolean Onjsalert (webview view, string URL, String message, final Jsresult result) {Alertdialog.builder B2 =
							New Alertdialog.builder (Mainactivity.this). Settitle ("hint"). Setmessage (Message). Setpositivebutton ("OK", New Alertdialog.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which)
								{//TODO auto-generated Method Stub result.confirm ();

				B2.setcancelable (FALSE);
				B2.create (); ();
			return true; @Override public boolean onjsconfirm (WebView view, string URL, string message, jsresult result) {//TODO Aut
			O-generated method stubs return super.onjsconfirm (view, URL, message, result); @Override public boolean onjsprompt (WebView view, string URL, String message, string defaultvalue, Jspromptr Esult REsult) {//TODO auto-generated Method stub return super.onjsprompt (View, URL, message, defaultvalue, result); } <span style= "White-space:pre" > </span>}

All right, this code is finished.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.