Android Development Study notes: talking about WebView

Source: Internet
Author: User

WebView (Network view) can load a display page, which can be viewed as a browser. It uses the WebKit rendering engine to load the Display Web page, implementing the WebView in the following two different ways:
Steps for the first method:
1. Instantiate the WebView component in the activity to: WebView WebView = new WebView (this);
2. Call WebView's Loadurl () method to set the page to display Wevview:
Internet use: Webview.loadurl ("http://www.cnblogs.com/chen-lhx/");
For local files: Webview.loadurl ("file:///android_asset/XX.html"); Local files are stored in the: Assets file
3. Call the activity's Setcontentview () method to display the page view
4. Use WebView point link to see a lot of pages later in order to let WebView support fallback function, need to overwrite the Activity class onkeydown () method, if do not do any processing, click the system fallback shear key, the entire browser will call finish () and end itself, Instead of retreating back to the previous page
5. You need to add permissions to the androidmanifest.xml file, or the Web page not available error will occur.
<uses-permission android:name= "Android.permission.INTERNET"/>
Here is a concrete example:


Mainactivity.java

 Packagecom.android.webview.activity; Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;ImportAndroid.webkit.WebView;  Public classMainactivityextendsActivity {PrivateWebView WebView; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //instantiating a WebView objectWebView =NewWebView ( This); //set the WebView property to be able to execute JavaScript scriptsWebview.getsettings (). setjavascriptenabled (true); //load Web pages that need to be displayedWebview.loadurl ("http://www.cnblogs.com/chen-lhx/"); //set up a Web viewSetcontentview (WebView); } @Override//Set Fallback//onkeydown (int keycoder,keyevent event) method that overrides the activity class     Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {         if((keycode = = Keyevent.keycode_back) &&Webview.cangoback ()) {Webview.goback ();//GoBack () indicates that the previous page of the WebView is returned            return true; }         return false; }


Add permissions to 17 rows in the Androidmanifest.xml file

 <?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.android.webview.activity"Android:versioncode= "1"Android:versionname= "1.0">    <USES-SDKandroid:minsdkversion= "Ten" />     <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name">        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application>    <uses-permissionAndroid:name= "Android.permission.INTERNET"/></Manifest>


Steps for the second method:
1. Declare webview in the layout file
2, in the activity of the instantiation of WebView
3, call WebView Loadurl () method, set Wevview to display the page
4. To enable WebView to respond to hyperlinks, call the Setwebviewclient () method to set the WebView view
5, with WebView point link to see a lot of pages later in order to let WebView support fallback function, need to overwrite the Activity class onkeydown () method, if do not do any processing, click the system fallback shear key, the entire browser will call finish () and end itself, Instead of retreating back to the previous page
6. You need to add permissions to the androidmanifest.xml file, or the Web page not available error occurs.
<uses-permission android:name= "Android.permission.INTERNET"/>
Here's a concrete example:


Mainactivity.java

 Packagecom.android.webview.activity; Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient;  Public classMainactivityextendsActivity {PrivateWebView WebView; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);         Setcontentview (R.layout.main); WebView=(WebView) Findviewbyid (R.id.webview); //set the WebView property to be able to execute JavaScript scriptsWebview.getsettings (). setjavascriptenabled (true); //load Web pages that need to be displayedWebview.loadurl ("http://www.cnblogs.com/chen-lhx/"); //set up a Web viewWebview.setwebviewclient (Newhellowebviewclient ()); } @Override//Set Fallback//onkeydown (int keycoder,keyevent event) method that overrides the activity class     Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {         if((keycode = = Keyevent.keycode_back) &&Webview.cangoback ()) {Webview.goback ();//GoBack () indicates that the previous page of the WebView is returned            return true; }         return false; }          //Web View    Private classHellowebviewclientextendswebviewclient {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true; }     } }


Main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    >       <WebViewAndroid:id= "@+id/webview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"        /></LinearLayout>


Add permissions to 17 rows in the Androidmanifest.xml file

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.android.webview.activity"Android:versioncode= "1"Android:versionname= "1.0">    <USES-SDKandroid:minsdkversion= "Ten" />     <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name">        <ActivityAndroid:name=". Mainactivity "Android:label= "@string/app_name">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>    </Application>    <uses-permissionAndroid:name= "Android.permission.INTERNET"/></Manifest>


Android Development Study notes: talking about WebView

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.