Android--webview controls

Source: Internet
Author: User

WebViewA Brief introduction:

WebView is generally used to show the HTML of the Android page, we generally call it HTML5 development;

WebView can make the Web page easy to embed into the app, but also can directly with JS call each other, through JSON or Gson format data call, can be very convenient and background interaction.

WebView has two methods:setwebchromeclient and setwebclient

Setwebclient: Mainly deal with parsing, rendering Web pages and other browsers do things;

Setwebchromeclient: Auxiliary webview Processing JavaScript dialog box, website icon, website title, loading progress, etc.

Webviewclient is to help WebView handle various notifications, request events.

second, simple use:

2.1. Set access network permissions in Androidmanifest.xml:

1 <uses-permission android:name="android.permission.INTERNET"/>
View Code

2.2. WebView Control:

1 <WebView2         android:id="@+id/view_html5_1"3          android:layout_height="fill_parent"4         android:layout_ Width="fill_parent"/>
View Code

2.3. Background Activity Program:

1  Packagecom.zdw.myhtml5_test;2 3 ImportAndroid.os.Bundle;4 ImportAndroid.webkit.WebView;5 Importandroid.webkit.WebViewClient;6 Importandroid.app.Activity;7 8  Public classMainactivityextendsActivity {9     PrivateWebView WebView;Ten     protected voidonCreate (Bundle savedinstancestate) { One         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); -Init ();//loading the initialization program -     }  the      - /*********************************************** "--initialization procedure--" *******************************************/     -     Private voidinit () { -WebView =(WebView) Findviewbyid (r.id.view_html5_1); +         //WebView Loading Web Resources -Webview.loadurl ("http://baidu.com"); +         //Overwrite WebView default use of third-party or system default browser to open Web page behavior, so that the Web page opened with WebView AWebview.setwebviewclient (Newwebviewclient () { at              Public Booleanshouldoverrideurlloading (WebView view, String URL) { -                 //The return value is true when the control goes to WebView open, and the system browser or third-party browser is called false -View.loadurl (URL);//Load URL -                 return true; -             } -         }); in     } -}
View Code

2.4, the program after the operation of the picture:

third, the trial way:3.1. load local Resources
1 webView = (WebView) Findviewbyid (R.id.webview); 2 webview.loadurl ("file:///android_asset/example.html");
View Code3.2. Load Web Resources
1 webView = (WebView) Findviewbyid (R.id.webview); 2 webview.loadurl ("http://baidu.com");
View CodeIv. Open the Web page in the program

It's been introduced, and there's no repetition here.

V. if there is JavaScript on the page visited, the WebView must be set to support JavaScript

It needs to be declared here.

1 // Enable JavaScript support 2 websettings settings = webview.getsettings (); 3 settings.setjavascriptenabled (true);
View Codesix, back page processing。 Requires WebView to overwrite the URL load, allowing it to automatically generate historical access records so that the visited sites can be accessed either forward or backward.
1 //rewrite physical keys--return logic2      Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {3         if(keycode==keyevent.keycode_back)4         {5             if(Webview.cangoback ())6             {7Webview.goback ();//back to previous page8                 return true;9             }Ten             Else One             { ASystem.exit (0);//Exit Program -             } -         } the         return Super. OnKeyDown (KeyCode, event); -}
View Code

Seven, judge page loading process

1Webview.setwebchromeclient (Newwebchromeclient () {2              Public voidOnprogresschanged (WebView view,intnewprogress) {3                 if(newprogress = = 100) {4                     //Web page loading complete5 6}Else {7                     //Load in8 9                 }Ten             } One});
View Code

Viii. use of the cache

Prioritize using caching:

1 webview.getsettings (). Setcachemode (Websettings.load_cache_else_network);
View Code

Do not use cache:

1 webview.getsettings (). Setcachemode (Websettings.load_no_cache);
View Code

Android--webview controls

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.