zoukankan      html  css  js  c++  java
  • Android中WebView使用总结

    1.在应用的配置文件中添加网络权限

    1     <!-- 添加使用WebView的需要的internet权限 -->
    2     <uses-permission android:name="android.permission.INTERNET"/>

    2.支持JavaScript

    1         //开启Javascript支持
    2         getSettings().setJavaScriptEnabled(true);

    3.支持页面图片加载

    1         //设置可以自动加载图片
    2         getSettings().setLoadsImagesAutomatically(true);

    4.重写shouldOverrideUrlLoading在WebView中显示新页面

     1         //设置直接在webview中查看网页
     2         setWebViewClient(new WebViewClient() {
     3 
     4             @Override
     5             public boolean shouldOverrideUrlLoading(WebView view, String url) {
     6                 view.loadUrl(url);
     7                 return true;
     8             }
     9             
    10         });

    完整的Demo代码列表如下:

    1.AndroidManifest.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.slowalker.webviewdemo"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="8"
     9         android:targetSdkVersion="18" />
    10 
    11     <!-- 添加使用WebView的需要的internet权限 -->
    12     <uses-permission android:name="android.permission.INTERNET"/>
    13     
    14     <application
    15         android:allowBackup="true"
    16         android:icon="@drawable/ic_launcher"
    17         android:label="@string/app_name"
    18         android:theme="@style/AppTheme" >
    19         <activity
    20             android:name="com.slowalker.webviewdemo.WebViewActivity"
    21             android:label="@string/app_name" >
    22             <intent-filter>
    23                 <action android:name="android.intent.action.MAIN" />
    24 
    25                 <category android:name="android.intent.category.LAUNCHER" />
    26             </intent-filter>
    27         </activity>
    28     </application>
    29 
    30 </manifest>
    View Code

    2.web_view_main.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".WebViewActivity" >
    10 
    11     <LinearLayout
    12         android:id="@+id/website"
    13         android:layout_alignParentTop="true"
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:orientation="horizontal"
    17         >
    18             <EditText 
    19                 android:id="@+id/url"
    20                 android:layout_width="wrap_content"
    21                 android:layout_height="wrap_content"
    22                 android:textSize="13dip"
    23                 android:hint="请输入网址"
    24                 android:layout_weight="9"
    25                 />
    26             <TextView
    27                 android:id="@+id/visit"
    28                 android:layout_width="wrap_content"
    29                 android:layout_height="wrap_content"
    30                 android:text="访问"
    31                 android:textSize="13dip"
    32                 android:layout_weight="1"
    33                 />
    34     </LinearLayout>
    35     <!-- 设置android WebView 不显示滚动条 -->
    36     <com.slowalker.webviewdemo.MyWebView 
    37         android:id="@+id/webview"
    38         android:layout_below="@id/website"
    39         android:layout_width="match_parent"
    40         android:layout_height="wrap_content"
    41         android:scrollbars="none"
    42         />
    43     
    44 </RelativeLayout>
    View Code

    3.WebViewActivity.java

     1 package com.slowalker.webviewdemo;
     2 
     3 import android.os.Bundle;
     4 import android.app.Activity;
     5 import android.view.KeyEvent;
     6 import android.view.Menu;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.webkit.WebView;
    10 import android.widget.EditText;
    11 import android.widget.TextView;
    12 
    13 public class WebViewActivity extends Activity {
    14     
    15     
    16     EditText urlText;
    17     TextView visitView;
    18     WebView webView;
    19     
    20     @Override
    21     protected void onCreate(Bundle savedInstanceState) {
    22         super.onCreate(savedInstanceState);
    23         setContentView(R.layout.web_view_main);
    24         urlText = (EditText) findViewById(R.id.url);
    25         visitView = (TextView) findViewById(R.id.visit);
    26         webView = (WebView) findViewById(R.id.webview);
    27         visitView.setOnClickListener(new OnClickListener() {
    28             
    29             @Override
    30             public void onClick(View v) {
    31                 String url = urlText.getText().toString();
    32                 //加载页面
    33                 webView.loadUrl(url);
    34             }
    35         });
    36     }
    37 
    38     
    39     //处理返回键为返回上一页
    40     @Override
    41     public boolean onKeyDown(int keyCode, KeyEvent event) {
    42         
    43         if (keyCode == KeyEvent.KEYCODE_BACK) {
    44             //回退上一页
    45             webView.goBack();
    46             return true;
    47         }
    48         return super.onKeyDown(keyCode, event);
    49     }
    50     
    51     
    52     
    53     
    54 
    55 }
    View Code

    4.MyWebView.java

     1 package com.slowalker.webviewdemo;
     2 
     3 import android.content.Context;
     4 import android.content.Intent;
     5 import android.net.Uri;
     6 import android.util.AttributeSet;
     7 import android.webkit.WebView;
     8 import android.webkit.WebViewClient;
     9 
    10 public class MyWebView extends WebView {
    11     
    12     public MyWebView(Context context) {
    13         super(context);
    14         init(context);
    15     }
    16 
    17     /**
    18      * 在xml文件中加载自定义view的时候,必须实现该构造方法。
    19      * @param context
    20      * @param attrs
    21      */
    22     public MyWebView(Context context, AttributeSet attrs) {
    23         super(context, attrs);
    24         init(context);
    25     }
    26     
    27     /**
    28      * 定制webview的参数设置
    29      * @param context
    30      */
    31     private void init(final Context context) {
    32         //开启Javascript支持
    33         getSettings().setJavaScriptEnabled(true);
    34         //设置可以自动加载图片
    35         getSettings().setLoadsImagesAutomatically(true);
    36         //设置直接在webview中查看网页
    37         setWebViewClient(new WebViewClient() {
    38 
    39             @Override
    40             public boolean shouldOverrideUrlLoading(WebView view, String url) {
    41                 view.loadUrl(url);
    42                 return true;
    43             }
    44             
    45         });
    46     }
    47     
    48 }
    View Code
  • 相关阅读:
    以您熟悉的编程语言为例完成一个hello/hi的简单的网络聊天程序——网络程序设计课第二次作业
    逆波兰表达式改写(C++)
    侯捷老师的C++代码:基于对象的类别之二带指针的成员函数 Mystring实现
    侯捷老师的C++代码: 基于对象之一 无指针类型 复数类实现
    雇员记录系统(C++)
    设计模式-Interpreter(行为模式) 使用解释器给用户提供一个一门定义语言的语法表示的解释器,通过该解释器解释语言中的句子。
    设计模式-Iterator(行为模式) 将聚合的遍历封装到一个类中
    设计模式-Chain of Responsibility (行为模式) 降低系统的耦合性
    设计模式-Visitor(行为模式) 一个类在不修改自己的同时增加了新的操作,存在问题是 1:破坏了封装性 2:扩展困难
    设计模式-Command(行为模式) 将一个请求封装到一个Command类中,提供一个处理对象Receiver,将Command由Invoker激活。
  • 原文地址:https://www.cnblogs.com/slowalker/p/3440074.html
Copyright © 2011-2022 走看看