webView 实现 与 javascript调用java方法(也称js调用native 方法) helloworld
2016-05-27 17:23
1161 查看
项目结构
![](http://img.blog.csdn.net/20160527171307628?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
效果图:
![](http://img.blog.csdn.net/20160527172051678?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
开发步骤:
1 创建普通的java类:WebAppInterface.java
2 activity_main.xml文件
3 MainActivity.java文件
4 assets/index.html文件
效果图:
开发步骤:
1 创建普通的java类:WebAppInterface.java
package com.exqw.adminx.myapplication; import android.content.Context; import android.webkit.JavascriptInterface; import android.widget.Toast; public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** Show a toast from the web page */ @JavascriptInterface public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } }
2 activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.exqw.adminx.myapplication.MainActivity" tools:showIn="@layout/activity_main"> <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
3 MainActivity.java文件
package com.exqw.adminx.myapplication; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("http://www.baidu.com"); // myWebView.loadUrl("file:///android_asset/hi.html"); myWebView.setWebViewClient(new MyWebViewClient()); WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); myWebView.addJavascriptInterface(new WebAppInterface(this), "Android"); } private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("www.example.com")) { // This is my web site, so do not override; let my WebView load the page return false; } // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); return true; } } }
4 assets/index.html文件
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /> <script type="text/javascript"> function showAndroidToast(toast) { Android.showToast(toast); } </script>
相关文章推荐
- LoadRunner+Android模所器录制脚本
- LoadRunner+Android模所器录制脚本
- APPCLOUD禁止滚动条
- [置顶] iOS关于NSString字符串操作
- iOS 再次生成一个.app.DSYM
- android 进程防止被杀死
- iOS 正则表达式(一)
- Android客户端性能优化
- Android异步任务处理之AsyncTaskLoader的使用
- Android基础之AsyncTask详解,巨细哟
- Android适配参考
- Android使用WindowManager构造悬浮view
- Androin学习笔记五十八:android触控,先了解MotionEvent
- Android几个bitmap相关的方法
- Android设计模式--适配器模式--Adapter入门
- android studio 真机运行的时候gradle 报错 AGPBI: {"kind":"SIMPLE","text":"Error: C
- 微信测试号的开发——网页授权
- Android中糟糕的AsyncTask
- 微信中无法接收post方式传参的form表单中的数据的问题
- Android studio 下JNI编程实例并生成so库