Common functions of webview

Source: Internet
Author: User

The webview component of android can be said to be quite powerful. The following are some common functions in the project:

I. background settings

Webview. setbackgroundcolor (0); // first set the background color to transparentwebview. setbackgroundresource (R. drawable. yourimage); // then set the background image

2. Obtain the webview webpage loading initialization and completion events
Steps:
1. Create your own webviewclient (inheriting the webviewclient class), such as webviewclient
2. Reload the onpagefinished (webview view, string URL) method in it. (This method is called after webview loading is complete.) This method puts what you want to do, after webview loading is complete.
3. associate your own webviewclient with webview through this method: webview. setwebviewclient (New webviewclient ();

Mwebview. setwebviewclient (New webviewclient () {@ overridepublic void onpagefinished (webview view, string URL) {// end super. onpagefinished (view, URL) ;}@ override public void onpagestarted (webview view, string URL, bitmap favicon) {// start super. onpagestarted (view, URL, favicon );}});

To monitor the loading progress, you need to create your own webchromeclient class, reload the onprogresschanged method, and then webview. setwebchromeclient (New mywebchromeclient:

Class mywebchromeclient extends webchromeclient {@ overridepublic void onprogresschanged (webview view, int newprogress) {// todo auto-generated method stubsuper. onprogresschanged (view, newprogress) ;}}example: public class webpageloader extends activity {final activity = This; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. getwindow (). requestfeature (window. feature_progress); setcontentview (R. layout. main); webview = (webview) findviewbyid (R. id. webview); webview. getsettings (). setjavascriptenabled (true); webview. getsettings (). setsuppzoom zoom (true); webview. setwebchromeclient (New webchromeclient () {public void onprogresschanged (webview view, int progress) {activity. settitle ("loading... "); activity. setprogress (Progress * 100); If (Progress = 100) activity. settitle (R. string. app_name) ;}}); webview. setwebviewclient (New webviewclient () {public void onreceivederror (webview view, int errorcode, string description, string failingurl) {// handle the error} public Boolean shouldoverrideurlloading (webview view, string URL) {view. loadurl (URL); Return true ;}}); webview. loadurl ("http://www.sohu.com ");}}

Layout file:

<?xml version="1.0" encoding="utf-8"?> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

It should be noted that a series of webview usage, such as webview. getsettings (). setjavascriptenabled (true); javscript can be used for setting;
Webview. getsettings (). setjavascriptenabled (true );
Webview. setscrollbarstyle (webview. scrollbars_outside_overlay );
Webview. sethorizontalscrollbarenabled (false );
Webview. getsettings (). setsuppzoom zoom (true );
Webview. getsettings (). setbuiltinzoomcontrols (true );
Webview. setinitialscale (70 );
Webview. sethorizontalscrollbaroverlay (true );
For more information, see the API
The progress bar can be written in the internal class after a new setwebchromeclient is generated.
Onprogresschanged event

Summary:
In the design of webview, The webview class is not required to do anything. Some chores are assigned to others, so that webview can focus on parsing and rendering. Webviewclient helps webview process various notifications and request events, including:
Onloadresource
Onpagestart
Onpagefinish
Onreceiveerror
Onreceivedhttpauthrequest

Webchromeclient is a dialog box that assists webview in processing Javascript, website icons, website titles, loading progress, etc.
Onclosewindow (disable webview)
Oncreatewindow ()
Onjsalert)
Onjsprompt
Onjsconfirm
Onprogresschanged
Onreceivedicon
Onreceivedtitle

About the strong support of JavaScript, here do not elaborate, set up can refer to the http://www.iteye.com/topic/376674 http://hi.baidu.com/crabzzz/blog/item/7e5545cbbb878990c91768d5.html

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.