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 () {      @Override public      boolean shouldoverrideurlloading (WebView View, String URL) {          view.loadurl (URL);          return true;      }  });

2. Invoking JavaScript through Java code

WebSettings websettings =   mwebview. getsettings ();       Websettings.setjavascriptenabled (TRUE); Mwebview.addjavascriptinterface (New Object () {public                   void clickonandroid () {                       mhandler.post (new Runnable () {public                           void run () {                               Webview.loadurl ("Javascript:wave ()");                           }                       });                                  

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

public boolean onKeyDown (int keycode, keyevent event) {               if (keycode = = keyevent.keycode_back) &&   Mwebview. CanGoBack ()) {                   webview.goback ();                          return true;               }               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 (true) at any scale;

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 JS
Setpluginsenabled (TRUE); Support Plugins
Setusewideviewport (FALSE); Adjust the picture to fit the size of the WebView
Setsupportzoom (TRUE); Support Scaling
Setlayoutalgorithm (Layoutalgorithm.single_column); Support for content re-layout
Supportmultiplewindows (); Multi-window
Setcachemode (websettings.load_cache_else_network); Close Cache in WebView
Setallowfileaccess (TRUE); Settings can access files
Setneedinitialfocus (TRUE); Set node for WebView when WebView calls Requestfocus

WebView Websettings.setbuiltinzoomcontrols (TRUE); Setting support Scaling
Setjavascriptcanopenwindowsautomatically (TRUE); Support to open a new window via JS

Setloadwithoverviewmode (TRUE); Zoom to the size of the screen
Setloadsimagesautomatically (TRUE); Supports automatic loading of pictures

Total solution of 9.WebViewClient method

Doupdatevisitedhistory (WebView view, String URL, boolean isreload)//(update history) Onformresubmission (WebView view, Message D Ontresend, Message Resend)//(Application re-requesting Web page data) Onloadresource (WebView view, String URL)//is called when the page resource is loaded, and each resource (the 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, int errorcode, 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)  //Override this method to have webview handle HTTPS requests.  onscalechanged (Webview view, float oldscale, float newscale)  //  ( WebView called when a change occurs)  onunhandledkeyevent (webview view, keyevent event)  //(called when the key event is not loaded)  shouldoverridekeyevent (webview view, keyevent event)//override this method to be able to handle key events in the browser.  shouldoverrideurlloading (Webview view, string url)  //in the click Request is the link is not called, Overriding this method returns true to indicate that clicking a link inside a webpage or jumping in the current webview does not jump to the browser. This functionWe can do a lot of things, such as we read to some special URL, so we can not open the address, cancel this 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.