Anaroid WebView API Detailed

Source: Internet
Author: User

Android provides WebView controls designed to browse the Web
When using WebView, loading the WebView control in the program, you can write webview to the layout, you can also dynamically new out WebView instances, you can set properties such as: color, font, the URL to visit, and so on. Use the Loadurl method to set the URL that the current webview needs to access. When creating WebView, the system has a default setting, which we can get by webview.getsettings.

WebView set some common properties, State methods:

websettings Common method:
setallowfileaccess Enable or disable WebView access to file data
Setblocknetworkimage whether the network image is displayed
Setbuiltinzoomcontrols setting supports scaling
Setcachemode setting buffered mode
Setdefaultfontsize Set the default font size
Setdefaulttextencodingname set the default encoding to use when decoding
Setfixedfontfamily set a fixed-use font
setjavasciptenabled Sets whether to support JavaScript
Setlayoutalgorithm settings layout
setlighttouchenabled settings with mouse activation is selected
Setsupportzoom setting supports zoom

webviewclient Common method:
doupdate visitedhistory update history
Onformresubmission The application re-requests the Web page data
Onloadresource load the resources provided by the specified address
onpagefinished the page loads
onpagestarted page starts loading
Onreceivederror Report error message
Onscalechanged WebView changed
shouldoverrideurlloading control the new connection opens in the current WebView

Webchromeclient Common methods:
Onclosewindow Close WebView
Oncreatewindow Creating WebView
Onjsalert Handling the Alert dialog box in JavaScript
Onjsconfirm handling the Confirm dialog box in JavaScript
Onjsprompt handling the prompt dialog box in JavaScript
Onprogresschanged Loading progress bar change
Onreceivedlcon page icon Change
Onreceivedtitle Page Title Change
Onrequestfocus WebView Show Focus

3. Android WebView Related knowledge
@ The following code sets the URL that the current WebView needs to access through the Loadurl method:
mwebview= (WebView) Findviewbyid (R.ID.WEBVIEW01);
Mwebview.loadurl ("http://www.sogou.com");

webview can load local resources and load them through the file protocol. The
uses websettings in Android to set some properties, states, and so on for WebView. When creating WebView, the system has a default setting that we can use to webview.getsettings to get this setting:
WebSettings websettings=mwebview.getsettings ();// The Get Object
WebSettings and WebView all exist in the same life cycle, and when WebView is destroyed, an exception is thrown if WebSettings is used again.
@ Use Webviewclient to finish customizing the Web browser in your application:
Webviewclient is a class that assists WebView in handling events such as notifications, requests, and so on. The Webviewclient object is specified by WebView's Setwebviewclient method.
WebView can help WebView browse the Web by overriding the Webviewclient method:
Public Boolean shouldoverrideurlloading
(WebView view, String URL) {

View. Loadurl (URL); return true;

}

Android WebView Instances
The instance uses EditText to enter the URL, uses the button to confirm the connection, and displays the Web page content with WebView.
Webviewclient is used here to assist webview with some events, so the pages we display are in a WebView control. You can set forward and backward through the GoBack and GoForward methods to check if you can go forward and back:
if ((Keycode==keyevent.keycode_back) &&mwebview.cangoback ())
{
Mwebview.goback ()//Return to previous page
return true;
}
example, you access an HTML page that contains a JavaScript dialog box through a home-made browser. By clicking on the "Warning, Reminder Dialog" button on the interface, the "dialog with Selection" button, the "Require user Input Dialog" button, the interface will pop up the dialog box "This is a warning dialog box", "More information please go to my Blog" normal button/Cancel button, "Input Dialog" normal button/Cancel button, A jump page. The Js HTML page is in the root directory of the code package (dialog.html).
We set up a Webchromeclient object for WebView through the Setwebchromeclient method, which assists the webview in processing the JS dialog box, listening for button events, We then pass our operation to JS processing through the confirm and cancel methods.
Package com.yarin.android.Examples_08_06;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.app.AlertDialog.Builder;
Import Android.content.DialogInterface;
Import Android.graphics.Bitmap;
......
public class Activity01 extends Activity
{
Private final String Debug_tag = "Activity01";
Private Button Mbutton;
Private EditText Medittext;
Private WebView Mwebview;
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Mbutton = (Button) Findviewbyid (R.ID.BUTTON01);
Medittext = (EditText) Findviewbyid (r.id.edittext01);
Mwebview = (WebView) Findviewbyid (R.ID.WEBVIEW01);
Setting up JavaScript script support
WebSettings websettings = Mwebview.getsettings ();
Websettings.setjavascriptenabled (TRUE);
Settings can access files
Websettings.setallowfileaccess (TRUE);
Setting support Scaling
Websettings.setbuiltinzoomcontrols (TRUE);
Set Webviewclient
Mwebview.setwebviewclient (New Webviewclient ()
{
public boolean shouldoverrideurlloading (WebView view, String URL)
{
View.loadurl (URL);
return true;
}
@Override
public void onpagefinished (WebView view, String URL)
{
super.onpagefinished (view, URL);
}
@Override
public void onpagestarted (WebView view, String URL, Bitmap favicon)
{
super.onpagestarted (view, URL, favicon);
}
});

Anaroid WebView API 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.