├── .classpath ├── .project ├── .settings ├── org.eclipse.core.resources.prefs └── org.eclipse.jdt.core.prefs ├── AndroidManifest.xml ├── README.md ├── bin ├── AndroidManifest.xml ├── WebView.apk ├── classes.dex ├── classes │ └── com │ │ └── example │ │ └── webview │ │ ├── BuildConfig.class │ │ ├── MainActivity$1.class │ │ ├── MainActivity.class │ │ ├── R$attr.class │ │ ├── R$drawable.class │ │ ├── R$id.class │ │ ├── R$layout.class │ │ ├── R$string.class │ │ └── R.class ├── res │ └── crunch │ │ ├── drawable-hdpi │ │ └── ic_launcher.png │ │ ├── drawable-mdpi │ │ └── ic_launcher.png │ │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ │ └── drawable-xxhdpi │ │ └── ic_launcher.png └── resources.ap_ ├── gen └── com │ └── example │ └── webview │ ├── BuildConfig.java │ └── R.java ├── ic_launcher-web.png ├── proguard-project.txt ├── project.properties ├── res ├── drawable-hdpi │ └── ic_launcher.png ├── layout │ └── activity_main.xml └── values │ └── strings.xml └── src └── com └── example └── webview └── MainActivity.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | WebView 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /.settings/org.eclipse.core.resources.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | encoding/=UTF-8 3 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 3 | org.eclipse.jdt.core.compiler.compliance=1.6 4 | org.eclipse.jdt.core.compiler.source=1.6 5 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### WebView 实现JS效果和a标签的点击事件 2 | 3 |

目前很多android app都可以显示web页面的界面,嵌入式开发,这个界面一般都是WebView这个控件加载出来的,学习该控件可以为你的app开发提升扩展性。

4 |

先说下WebView的一些优点:

5 |
    6 |
  1. 可以直接显示和渲染web页面,直接显示网页
  2. 7 |
  3. webview可以直接用html文件(网络上或本地assets中)作布局
  4. 8 |
  5. 和JavaScript交互调用
  6. 9 |
  7. 网页标签的点击事件
  8. 10 |
11 |

效果:(网页顶部是JS效果滚动,4个模块可以实现点击事件,可看到信息提示)

12 |

       

13 |

 

14 | -------------------------------------------------------------------------------- /bin/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 17 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /bin/WebView.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/WebView.apk -------------------------------------------------------------------------------- /bin/classes.dex: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes.dex -------------------------------------------------------------------------------- /bin/classes/com/example/webview/BuildConfig.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/BuildConfig.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/MainActivity$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/MainActivity$1.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/MainActivity.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/MainActivity.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/R$attr.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/R$attr.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/R$drawable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/R$drawable.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/R$id.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/R$id.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/R$layout.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/R$layout.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/R$string.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/R$string.class -------------------------------------------------------------------------------- /bin/classes/com/example/webview/R.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/classes/com/example/webview/R.class -------------------------------------------------------------------------------- /bin/res/crunch/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/res/crunch/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /bin/res/crunch/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/res/crunch/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /bin/res/crunch/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/res/crunch/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /bin/res/crunch/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/res/crunch/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /bin/resources.ap_: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/bin/resources.ap_ -------------------------------------------------------------------------------- /gen/com/example/webview/BuildConfig.java: -------------------------------------------------------------------------------- 1 | /** Automatically generated file. DO NOT MODIFY */ 2 | package com.example.webview; 3 | 4 | public final class BuildConfig { 5 | public final static boolean DEBUG = true; 6 | } -------------------------------------------------------------------------------- /gen/com/example/webview/R.java: -------------------------------------------------------------------------------- 1 | /* AUTO-GENERATED FILE. DO NOT MODIFY. 2 | * 3 | * This class was automatically generated by the 4 | * aapt tool from the resource data it found. It 5 | * should not be modified by hand. 6 | */ 7 | 8 | package com.example.webview; 9 | 10 | public final class R { 11 | public static final class attr { 12 | } 13 | public static final class drawable { 14 | public static final int ic_launcher=0x7f020000; 15 | } 16 | public static final class id { 17 | public static final int webview=0x7f050000; 18 | } 19 | public static final class layout { 20 | public static final int activity_main=0x7f030000; 21 | } 22 | public static final class string { 23 | public static final int app_name=0x7f040000; 24 | public static final int hello_world=0x7f040001; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/ic_launcher-web.png -------------------------------------------------------------------------------- /proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | -------------------------------------------------------------------------------- /res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DickyQie/android-webview/7fb4d012cf0bb0f55669576c14a99b1bda0e4046/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | WebView 5 | Hello world! 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/com/example/webview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.webview; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.app.Activity; 5 | import android.os.Bundle; 6 | import android.webkit.WebSettings; 7 | import android.webkit.WebView; 8 | import android.webkit.WebViewClient; 9 | import android.widget.Toast; 10 | 11 | /**** 12 | * 13 | * WebView 实现JS效果和a标签的点击事件 14 | * 15 | * @author zq 16 | * 17 | */ 18 | public class MainActivity extends Activity { 19 | 20 | public String URL = "http://bajie.zhangwoo.cn/app.php?platform=android&appkey=5a379b5eed8aaae531df5f60b12100cfb6dff2c1&c=travel&a=home"; 21 | WebView webView; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | setContentView(R.layout.activity_main); 27 | webView = (WebView) findViewById(R.id.webview); 28 | webView.loadUrl(URL); 29 | initView(); 30 | 31 | } 32 | @SuppressLint("SetJavaScriptEnabled") private void initView() { 33 | // TODO Auto-generated method stub 34 | webView.requestFocus(); 35 | webView.setHorizontalScrollBarEnabled(true); 36 | webView.setVerticalScrollBarEnabled(true); 37 | WebSettings web = webView.getSettings(); 38 | web.setJavaScriptEnabled(true);// 启用支持javascript 39 | web.setBuiltInZoomControls(true); 40 | web.setSupportZoom(true); // 是否支持屏幕双击缩放,但是下边的是前提 41 | web.setDefaultTextEncodingName("utf-8");// 设置编码格式 42 | // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 43 | webView.setWebViewClient(new WebViewClient() { 44 | @Override 45 | public boolean shouldOverrideUrlLoading(WebView view, String url) { 46 | // TODO Auto-generated method stub 47 | // 返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 48 | 49 | if (url.indexOf("zwapp://showlist/?tab=zhoubian") != -1) { 50 | 51 | Toast.makeText(getApplicationContext(), "周边游", 1).show(); 52 | 53 | } else if (url.indexOf("zwapp://showlist/?tab=gonglue") != -1) { 54 | 55 | Toast.makeText(getApplicationContext(), "旅游攻略", 1).show(); 56 | } else if (url.indexOf("zwapp://showlist/?tab=zhaiguo") != -1) { 57 | 58 | Toast.makeText(getApplicationContext(), "摘果", 1).show(); 59 | } else if (url.indexOf("zwapp://showlist/?tab=gongyuan") != -1) { 60 | 61 | Toast.makeText(getApplicationContext(), "主题公园", 1).show(); 62 | 63 | } else { 64 | 65 | } 66 | return true; 67 | 68 | } 69 | }); 70 | 71 | } 72 | 73 | } 74 | --------------------------------------------------------------------------------