WebView usage Summary

Source: Internet
Author: User

WebView usage Summary
1) Add permission: The permission "android. permission. INTERNET" must be used in AndroidManifest. xml; otherwise, the Web page not available error may occur.
2) generate a WebView component in the Activity: WebView webView = new WebView (this); or add the webview control to the layout file of the activity:

Android: id = "@ + id/wv"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "@ string/hello"

/>


3) set basic WebView information:
If the accessed page contains Javascript, webview must be set to support Javascript.
Webview. getSettings (). setJavaScriptEnabled (true );
Touch focus Function
RequestFocus ();
Cancel scroll bar
This. setScrollBarStyle (SCROLLBARS_OUTSIDE_OVERLAY );

4) set the web page to be displayed in WevView:
For Internet: webView. loadUrl ("http://www.google.com ");

Local file: webView. loadUrl ("file: // android_asset/XX.html"); local files are stored in the assets file.


5) If you want to click the link for processing by yourself, instead of the link in the browser of the new Android system.Add an event listening object (WebViewClient) to WebView and override some of the methods:

ShouldOverrideUrlLoading: Response to the hyperlink button on the webpage. When you press a connection, WebViewClient will call this method and pass the parameter: the url to be pressed. For example, when a number of embedded web pages in webview is clicked, it will automatically consider this as a telephone request and pass the url: tel: 123, if you do not want this, you can rewrite the shouldOverrideUrlLoading function to solve the problem:

Public boolean shouldOverrideUrlLoading (WebView view, String url) {if (url. indexOf ("tel:") <0) {// a number is displayed on the page, causing the connection call view. loadUrl (url);} return true ;}



6) to allow WebView to load assets from the apk file, the Android SDK provides a schema prefixed with "file: // android_asset /". When WebView encounters such a schema, it finds the content in the assets Directory of the current package. Above "file: // android_asset/demo.html"


7) the Java object and method to be bound in the addJavascriptInterface method must run in another thread and cannot be run in the Construction thread. This is also the purpose of using Handler.

8. When the div layer is used in webview, overlapping operations may occur. You can only use the dom method to delete the div.


Webview and js Interaction
 wv.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");private final class DemoJavaScriptInterface    { DemoJavaScriptInterface(){}     public void clickonAndroid( final String order){         mHandler.post(newRunnable(){             @Override             public void run(){                       jsonText="{"name":""+order+""}";                wv.loadUrl("javascript:wave("+jsonText+")");             }         });     }}




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.