Android WebView Use detailed

Source: Internet
Author: User

1. Instead of invoking the system browser when opening a Web page, it is shown in this webview:

Mwebview.setwebviewclient (new webviewclient () {public      boolean Shouldoverrideurlloading (WebView view, String URL) {          returntrue;      }  });

2. Invoking JavaScript through Java code

WebSettings websettings =   mwebview. getsettings ();       Websettings.setjavascriptenabled (true); Mwebview.addjavascriptinterface (newpublicvoid clickonandroid () {                       mhandler.post ( New  Public void Run () {                               Webview.loadurl ("Javascript:wave ()");}});}               , "demo");

3. When you press the back key, do not exit the program but return to the previous browse page:

 Public boolean onKeyDown (intif (keycode = = keyevent.keycode_back) &&   Mwebview. CanGoBack ()) {                   returntrue;               return Super. OnKeyDown (KeyCode, event);           }

4. When the page is opened, the adaptive screen:

WebSettings websettings =   mwebview. getsettings ();       Websettings.setusewideviewport (true);//Set this property to scale websettings.setloadwithoverviewmode at any scale (true );

5. The page supports zooming:

WebSettings websettings =   mwebview. getsettings ();       Websettings.setjavascriptenabled (true);  Websettings.setbuiltinzoomcontrols (true); Websettings.setsupportzoom (true);

6. If the user is required to manually enter a user name, password, or other webview, the WebView must set the support acquisition gesture focus.

Webview.requestfocusfromtouch ();

7.WebView loading interface mainly calls three methods: Loadurl, LoadData, Loaddatawithbaseurl.

1, Loadurl directly load the webpage, the picture and displays. (Local or Web pages, pictures, gifs) 2, LoadData display text and picture content (simulator 1.5, 1.6) 3, loaddatawithbase display Text and picture content (supports multiple emulator versions)

Introduction to the common methods of 8.WebSettings

Setjavascriptenabled (true);//Support JSSetpluginsenabled (true);//Support PluginsSetusewideviewport (false);//adjust the picture to fit the size of the WebViewSetsupportzoom (true);//Support ScalingSetlayoutalgorithm (Layoutalgorithm.single_column);//support for content re-layoutSupportmultiplewindows ();//Multi-WindowSetcachemode (websettings.load_cache_else_network);//Close cache in WebViewSetallowfileaccess (true);//settings can access filesSetneedinitialfocus (true); Set node for WebView when WebView calls RequestfocusWebView Websettings.setbuiltinzoomcontrols (true);//Setting Support ScalingSetjavascriptcanopenwindowsautomatically (true);//support to open a new window via JS Setloadwithoverviewmode (TRUE);//Zoom to screen sizeSetloadsimagesautomatically (true);//supports automatic loading of pictures

Total solution of 9.WebViewClient method

Doupdatevisitedhistory (WebView view, String URL,BooleanIsreload)//(update history)Onformresubmission (WebView view, message dontresend, message resend)//(Application re-requests web page data)Onloadresource (WebView view, String URL)//called when a page resource is loaded, and each resource (a slice) is loaded once. Onpagestarted (WebView view, String URL, Bitmap favicon)//This event is the start of loading the page call, usually we can set a loading in this page, tell the user program is waiting for the network response. Onpagefinished (WebView view, String URL)//called at the end of page loading. Similarly, we know that a page loading is complete, so we can close the loading bar, switch the program action. Onreceivederror (WebView view,intErrorCode, string description, String failingurl)//(report error message)Onreceivedhttpauthrequest (WebView view, Httpauthhandler handler, String host,string Realm)//(Get Return Information authorization request)Onreceivedsslerror (WebView view, Sslerrorhandler handler, sslerror error)//overriding this method allows WebView to handle HTTPS requests. Onscalechanged (WebView view,floatOldscale,floatNewscale)//(called when WebView changes)Onunhandledkeyevent (WebView view, KeyEvent event)//(called when the key event is not loaded)Shouldoverridekeyevent (WebView view, KeyEvent event)//override this method to handle key events in the browser. Shouldoverrideurlloading (WebView view, String URL)//The Click Request is a link is only called, overriding this method returns true to indicate that clicking on the link in the page or in the current WebView jump, do not jump to the browser side. This function we can do a lot of things, such as we read to some special URL, so we can not open the address, cancel the operation, for the pre-defined other operations, which is very necessary for a program. 

Android WebView Use detailed

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: info-contact@alibabacloud.com 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.