Android中WebView使用总结

摘要:
1.在应用程序配置文件1˂!

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

Android中WebView使用总结第1张Android中WebView使用总结第2张
 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

Android中WebView使用总结第3张Android中WebView使用总结第4张
 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

Android中WebView使用总结第5张Android中WebView使用总结第6张
 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

Android中WebView使用总结第7张Android中WebView使用总结第8张
 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

免责声明:文章转载自《Android中WebView使用总结》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇谷歌浏览器开启开启flash方法python 简单图像识别--验证码下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

android绘图—Paint path 旋转

http://meteor6789.blog.163.com/blog/static/35040733201111193535153/ Piant 看一段代码: mPaint = new Paint();mPaint.setAntiAlias(true);//锯齿mPaint.setDither(true);//mPaint.setColor(0xFF3...

如何检查Android网络连接状态

在发送任何HTTP请求前最好检查下网络连接状态,这样可以避免异常。这个教程将会介绍怎样在你的应用中检测网络连接状态。 创建新的项目 1.在Eclipse IDE中创建一个新的项目并把填入必须的信息。File->New->Android Project 2.创建新项目后的第一步是要在AndroidManifest.xml文件中添加必要的权限...

webos项目中EF仓储模式的代码

参考了网上EF数据处理的代码写成 在UPDATE的方法不太好,性能可能有问题。大家如果有什么好的方法可以给我留言。 暂时没有想到 接口类 using System; using System.Collections.Generic; namespace Model.EF { public interface IRepository<TEnt...

大文件上传 之 改版了的SlickUpload.HttpUploadModule(Krystalware.SlickUpload.dll)

以下代码中所注释的部分是所改版的地方。:)Krystalware.SlickUpload.dll/Files/bigmouthz/SlickUpload.rar------------------------------------------------------using System;using System.Collections;using S...

TOMCAT原理详解及请求过程(转)

https://www.cnblogs.com/hggen/p/6264475.html TOMCAT原理详解及请求过程 Tomcat: Tomcat是一个JSP/Servlet容器。其作为Servlet容器,有三种工作模式:独立的Servlet容器、进程内的Servlet容器和进程外的Servlet容器。 Tomcat目录: tomcat|---bin:...

PSR

PSR是PHP Standards Recommendation的简称,这个是php-fig组织制定的一套规范。 PSR-1 PHP标签:PHP代码必须放在<?php ?>标签或<?= ?>标签中。 编码:PHP文件必须使用无BOM的UTF-8编码。 副作用:一个PHP文件可以定义符号(比如类、函数、常量等),或者执行只有唯一副...