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.google.com");
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

  1. Package com.android.webview.activity;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import android.view.KeyEvent;
  5. Import Android.webkit.WebView;
  6. Public class Mainactivity extends Activity {
  7. private WebView WebView;
  8. @Override
  9. public void OnCreate (Bundle savedinstancestate) {
  10. super.oncreate (savedinstancestate);
  11. //Instantiate WebView object
  12. WebView = New WebView (this);
  13. //Set the WebView property to be able to execute JavaScript scripts
  14. Webview.getsettings (). setjavascriptenabled (true);
  15. //Load the Web page that needs to be displayed
  16. Webview.loadurl ("http://www.51cto.com/");
  17. //Set up Web View
  18. Setcontentview (WebView);
  19. }
  20. @Override
  21. //Set fallback
  22. //onkeydown (int keycoder,keyevent event) method covering activity class
  23. Public boolean onKeyDown (int keycode, keyevent event) {
  24. if ((keycode = = keyevent.keycode_back) && webview.cangoback ()) {
  25. Webview.goback (); //goback () indicates that the previous page of the WebView is returned
  26. return true;
  27. }
  28. return false;
  29. }

Add permissions to 17 rows in the Androidmanifest.xml file

  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.webview.activity"
  4. android:versioncode="1"
  5. android:versionname="1.0">
  6. <uses-sdk android:minsdkversion="ten" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=". Mainactivity "
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="Android.intent.action.MAIN" />
  12. <category android:name="Android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </Activity>
  15. </Application>
  16. <uses-permission android:name="Android.permission.INTERNET"/>
  17. </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

  1. Package com.android.webview.activity;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import android.view.KeyEvent;
  5. Import Android.webkit.WebView;
  6. Import android.webkit.WebViewClient;
  7. Public class Mainactivity extends Activity {
  8. private WebView WebView;
  9. @Override
  10. public void OnCreate (Bundle savedinstancestate) {
  11. super.oncreate (savedinstancestate);
  12. Setcontentview (R.layout.main);
  13. WebView = (webview) Findviewbyid (R.id.webview);
  14. //Set the WebView property to be able to execute JavaScript scripts
  15. Webview.getsettings (). setjavascriptenabled (true);
  16. //Load the Web page that needs to be displayed
  17. Webview.loadurl ("http://www.51cto.com/");
  18. //Set up Web View
  19. Webview.setwebviewclient (new Hellowebviewclient ());
  20. }
  21. @Override
  22. //Set fallback
  23. //onkeydown (int keycoder,keyevent event) method covering activity class
  24. Public boolean onKeyDown (int keycode, keyevent event) {
  25. if ((keycode = = keyevent.keycode_back) && webview.cangoback ()) {
  26. Webview.goback (); //goback () indicates that the previous page of the WebView is returned
  27. return true;
  28. }
  29. return false;
  30. }
  31. //web View
  32. private class Hellowebviewclient extends Webviewclient {
  33. @Override
  34. Public Boolean shouldoverrideurlloading (WebView view, String URL) {
  35. View.loadurl (URL);
  36. return true;
  37. }
  38. }
  39. }

Main.xml

  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <WebView
  8. android:id="@+id/webview"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent"
  11. />
  12. </linearlayout>

Add permissions to 17 rows in the Androidmanifest.xml file

  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.webview.activity"
  4. android:versioncode="1"
  5. android:versionname="1.0">
  6. <uses-sdk android:minsdkversion="ten" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=". Mainactivity "
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="Android.intent.action.MAIN" />
  12. <category android:name="Android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </Activity>
  15. </Application>
  16. <uses-permission android:name="Android.permission.INTERNET"/>
  17. </manifest>

Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://liangruijun.blog.51cto.com/3061169/647456

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.