Anaroid WebView details

Source: Internet
Author: User

Resource Description:

1. android provides the webView Control for Webpage Browsing.
Then, load the webView control in the program and set attributes, such as color, Font, and URL to be accessed. Use the loadUrl method to set the URL to be accessed by the current webView. When creating a WebView, the system has a default setting. We can use WebView. getSettings to get this setting.
 

2. The following describes how to set common attributes and statuses:
Common WebSettings methods:
SetAllowFileAccess enable or disable WebView from accessing File data
SetBlockNetworkImage: whether to display network images
SetBuiltInZoomControls: whether scaling is supported
SetCacheMode: Set the buffer mode.
SetDefaultFontSize: Set the default font size.
SetDefaultTextEncodingName sets the default encoding used for decoding.
SetFixedFontFamily: set fixed Fonts
SetJavaSciptEnabled: Set whether Javascript is supported
SetLayoutAlgorithm
SetLightTouchEnabled sets the option to activate with the mouse
Whether the setsuppzoom zoom setting supports zoom

Common WebViewClient methods:
DoUpdate VisitedHistory update history
OnFormResubmission application re-Requests webpage data
OnLoadResource loads resources provided by a specified address
OnPageFinished web page loaded
OnPageStarted web page loading
OnReceivedError reports error messages
OnScaleChanged WebView changed
ShouldOverrideUrlLoading controls the opening of new connections in the current WebView

Common WebChromeClient methods:
OnCloseWindow disable WebView
OnCreateWindow create WebView
OnJsAlert handles Alert dialog box in Javascript
OnJsConfirm processing Confirm in Javascript dialog box
OnJsPrompt processing the Prompt dialog box in Javascript
OnProgressChanged loading progress bar changed
OnReceivedlcon webpage icon change
OnReceivedTitle webpage Title change
OnRequestFocus WebView display focus
 

3. Knowledge about android webView
@ The following Code sets the URL to be accessed by the current webView using the loadUrl method:
MWebView = (WebView) findViewById (R. id. WebView01 );
MWebView. loadUrl (http://www.sogou.com ");
@ In android, WebSettings is used to set some attributes and statuses of WebView. When creating a WebView, the system has a default setting. We can use WebView. getSettings to get this setting:
WebSettings webSettings = mWebView. getSettings (); // retrieves an object
WebSettings and WebView both exist in the same lifecycle. If WebView is destroyed and WebSettings is used again, an exception is thrown.
@ Use WebViewClient to customize web browsing programs in applications:
WebViewClient is a class that assists WebView in handling various events such as notifications and requests. Use the setWebViewClient method of WebView to specify the WebViewClient object.
WebView can help WebView browse Web pages by overwriting WebViewClient:
Public Boolean shouldOverrideUrlLoading
(WebView view, String url) {view. loadUrl (url); return true ;}
 

 

4. android webView instance
Enter the URL using EditText In the instance, use a Button to confirm the connection, and use webView to display the webpage content.
WebViewClient is used to assist WebView in processing some events. Therefore, the webpage we display is in a WebView control. You can use the goBack and goForward methods to set forward and backward, and first check whether forward and backward are allowed:
If (keyCode = keyEvent. KEYCODE_BACK) & mWebView. canGoBack ())
{
MWebView. goBack () // return to the previous page
Return true;
}
In this example, you can access an html page that contains the javascript dialog box through a self-made browser. Click the "Warning, reminder dialog box" button, "selected dialog box" button, and "Required User input dialog box" button on the interface, the dialog box "this is a warning dialog box", "Go to my blog for more information", "OK", "enter", "OK", and "cancel" are displayed, respectively. page. The Js HTML page is in the root directory of the code package (dialog.html ).
We use the setWebChromeClient method to set a WebChromeClient object for WebView, assist WebView in processing js dialogs, listen to button events, and then pass our operations to js for 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 );
// Configure JavaScript scripts
WebSettings webSettings = mWebView. getSettings ();
WebSettings. setJavaScriptEnabled (true );
// Set File Access
WebSettings. setAllowFileAccess (true );
// Supports 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 );
}
});

Related Article

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.