Android advanced 2 webview (browser)

Source: Internet
Author: User

I. Overview
Webview is a view that displays Web pages. It is based on your web browser or content that is only displayed in your activity.
It uses the WebKit rendering engine to display webpages. It includes some methods: browsing records forward and backward, enlarging, downgrading, and text search.
Implement built-in zoom in and out using websettings. setbuiltinzoomcontrols (Boolean ).
Note: To enable your activity to access the Internet, you must add Internet permissions to your android manifest file to load webpages in webview.
<Uses-Permission Android: Name = "android. Permission. Internet"/>
Step 2:
1. instantiate the webview component in the activity: webview = new webview (this );
You can also declare webview in the layout file and instantiate webview in the activity.
2. Call the webview loadurl () method to set the web page to be displayed in wevview:
For Internet: webview. loadurl ("http://www.baidu.com ");
Local file: webview. loadurl ("file: // android_asset/xx.html"); local files are stored in the assets file.
3. Call the setcontentview () method of activity to display the webpage view.
3. Notes
Pay attention to the following points during development:
1. The permission "android. Permission. Internet" must be added to androidmanifest. xml ".
<textarea class="java" name="code" readonly><Uses-Permission Android: Name = "android. Permission. Internet"/></textarea>
2. If the accessed page contains JavaScript, webview must be set to support JavaScript.
<textarea class="java" name="code" readonly>Webview. getsettings (). setjavascriptenabled (true );</textarea>
3. If you want to click the link on the page to continue responding in the current browser, instead of making a response in the browser of the new Android system, you must overwrite the webviewclient object of webview.
<textarea class="java" style="width: 1078px; height: 129px; margin: 1.11111px;" name="code" readonly>Webview. setwebviewclient (New webviewclient () {<br/> Public Boolean shouldoverrideurlloading (webview view, string URL) {<br/> View. loadurl (URL); <br/> return true; <br/>}< br/> });</textarea>

4. if you do not do any processing, browse the Web page and click the system "back" key, the entire browser will call finish () to end itself. If you want to browse the Web page back rather than launch the browser, the back event needs to be processed and consumed in the current activity.

Public Boolean onkeydown (INT keycode, keyevent event) {<br/> If (keycode = keyevent. keycode_back) & webview. cangoback () {<br/> webview. goback (); // Goback () indicates that the previous page of webview is returned <br/> return true; <br/>}< br/> return false; <br/>}

Specific implementation:

Package xiaosi. webview; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. keyevent; <br/> Import android. webKit. webview; <br/> Import android. webKit. webviewclient; </P> <p> public class mywebviewactivity extends activity {<br/> private webview; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> webview = (webview) findviewbyid (R. id. webview); <br/> // sets the webview attribute to execute a Javascript script <br/> webview. getsettings (). setjavascriptenabled (true); <br/> // load the webpage to be displayed <br/> webview. loadurl ("http://www.baidu.com/"); <br/> // sets the Web View <br/> webview. setwebviewclient (New hellowebviewclient ()); <br/>}</P> <p> @ override <br/> // set rollback <br/> Public Boolean onkeydown (INT keycode, keyevent event) {<br/> If (keycode = keyevent. keycode_back) & webview. cangoback () {<br/> webview. goback (); // Goback () indicates that the previous page of webview is returned <br/> return true; <br/>}< br/> return false; <br/>}</P> <p> // Web View <br/> private class hellowebviewclient extends webviewclient {<br/> @ override <br/> Public Boolean shouldoverrideurlloading (webview view, string URL) {<br/> View. loadurl (URL); <br/> return true; <br/>}< br/>}

Main. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Orientation = "vertical"> </P> <p> <webview <br/> Android: id = "@ + ID/webview" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"/> </P> <p> </linearlayout>

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.