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
- Package com.android.webview.activity;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.KeyEvent;
- Import Android.webkit.WebView;
- Public class Mainactivity extends Activity {
- private WebView WebView;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- //Instantiate WebView object
- WebView = New WebView (this);
- //Set the WebView property to be able to execute JavaScript scripts
- Webview.getsettings (). setjavascriptenabled (true);
- //Load the Web page that needs to be displayed
- Webview.loadurl ("http://www.51cto.com/");
- //Set up Web View
- Setcontentview (WebView);
- }
- @Override
- //Set fallback
- //onkeydown (int keycoder,keyevent event) method covering activity class
- Public boolean onKeyDown (int keycode, 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"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.webview.activity"
- android:versioncode="1"
- android:versionname="1.0">
- <uses-sdk android:minsdkversion="ten" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=". Mainactivity "
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- </Activity>
- </Application>
- <uses-permission android: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
- Package com.android.webview.activity;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import android.view.KeyEvent;
- Import Android.webkit.WebView;
- Import android.webkit.WebViewClient;
- Public class Mainactivity extends Activity {
- private WebView WebView;
- @Override
- public void OnCreate (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 scripts
- Webview.getsettings (). setjavascriptenabled (true);
- //Load the Web page that needs to be displayed
- Webview.loadurl ("http://www.51cto.com/");
- //Set up Web View
- Webview.setwebviewclient (new Hellowebviewclient ());
- }
- @Override
- //Set fallback
- //onkeydown (int keycoder,keyevent event) method covering activity class
- Public boolean onKeyDown (int keycode, 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 class Hellowebviewclient extends Webviewclient {
- @Override
- Public Boolean shouldoverrideurlloading (WebView view, String URL) {
- View.loadurl (URL);
- return true;
- }
- }
- }
Main.xml
- <? XML version= "1.0" encoding="Utf-8"?>
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <WebView
- android: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"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.webview.activity"
- android:versioncode="1"
- android:versionname="1.0">
- <uses-sdk android:minsdkversion="ten" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=". Mainactivity "
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="Android.intent.action.MAIN" />
- <category android:name="Android.intent.category.LAUNCHER" />
- </intent-filter>
- </Activity>
- </Application>
- <uses-permission android:name="Android.permission.INTERNET"/>
- </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