原文链接:http://liangruijun.blog.51cto.com/3061169/647456/
- 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);
- //实例化WebView对象
- webview = new WebView(this);
- //设置WebView属性,能够执行Javascript脚本
- webview.getSettings().setJavaScriptEnabled(true);
- //加载需要显示的网页
- webview.loadUrl("http://www.51cto.com/");
- //设置Web视图
- setContentView(webview);
- }
- @Override
- //设置回退
- //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
- webview.goBack(); //goBack()表示返回WebView的上一页面
- return true;
- }
- return false;
- }
- <?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="10" />
- <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>
- 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);
- //设置WebView属性,能够执行Javascript脚本
- webview.getSettings().setJavaScriptEnabled(true);
- //加载需要显示的网页
- webview.loadUrl("http://www.51cto.com/");
- //设置Web视图
- webview.setWebViewClient(new HelloWebViewClient ());
- }
- @Override
- //设置回退
- //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
- webview.goBack(); //goBack()表示返回WebView的上一页面
- return true;
- }
- return false;
- }
- //Web视图
- private class HelloWebViewClient extends WebViewClient {
- @Override
- public boolean shouldOverrideUrlLoading(WebView view, String url) {
- view.loadUrl(url);
- return true;
- }
- }
- }
- <?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>
- <?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="10" />
- <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>