Android:WebView与Javascript交互(相互调用参数、传值)
16lz
2021-12-04
Android中可以使用WebView加载网页,同时Android端的java代码可以与网页上的javascript代码之间相互调用。
效果图:
(一)Android部分:
布局代码:
[html] view plain copy- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:orientation="vertical"
- android:padding="8dp"
- tools:context=".MainActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <EditText
- android:id="@+id/input_et"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:singleLine="true"
- android:layout_weight="1"
- android:hint="请输入信息"/>
- <Button
- android:text="Java调用JS"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="sendInfoToJs"/>
- </LinearLayout>
- <WebView
- android:id="@+id/webView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- </LinearLayout>
- /**
- *AndroidWebView与Javascript交互。
- */
- publicclassMainActivityextendsActionBarActivity{
- privateWebViewwebView;
- @SuppressLint({"SetJavaScriptEnabled","AddJavascriptInterface"})
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- webView=(WebView)findViewById(R.id.webView);
- webView.setVerticalScrollbarOverlay(true);
- //设置WebView支持JavaScript
- webView.getSettings().setJavaScriptEnabled(true);
- Stringurl="http://192.168.1.27/js_17_android_webview.html";
- webView.loadUrl(url);
- //在js中调用本地java方法
- webView.addJavascriptInterface(newJsInterface(this),"AndroidWebView"); //重要的码
- //添加客户端支持
- webView.setWebChromeClient(newWebChromeClient());
- }
- privateclassJsInterface{
- privateContextmContext;
- publicJsInterface(Contextcontext){
- this.mContext=context;
- }
- //在js中调用window.AndroidWebView.showInfoFromJs(name),便会触发此方法。 ***************
- @JavascriptInterface
- publicvoidshowInfoFromJs(Stringname){
- Toast.makeText(mContext,name,Toast.LENGTH_SHORT).show();
- }
- }
- //在java中调用js代码
- publicvoidsendInfoToJs(Viewview){
- Stringmsg=((EditText)findViewById(R.id.input_et)).getText().toString();
- //调用js中的函数:showInfoFromJava(msg)
- webView.loadUrl("javascript:showInfoFromJava('"+msg+"')");
- }
- }
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
- <metahttp-equiv="Content-Language"content="zh-cn"/>
- <title>AndroidWebView与Javascript交互</title>
- <head>
- <style>
- body{background-color:#e6e6e6}
- .rect
- {
- color:white;
- font-family:Verdana,Arial,Helvetica,sans-serif;
- font-size:16px;
- width:100px;
- padding:6px;
- background-color:#98bf21;
- text-decoration:none;
- text-align:center;
- border:none;
- cursor:pointer;
- }
- .inputStyle{font-size:16px;padding:6px}
- </style>
- </head>
- <body>
- <p>测试AndroidWebView与Javascript交互</p>
- <inputid="name_input"class="inputStyle"type="text"/>
- <aclass="rect"onclick="sendInfoToJava()">JS调用Java</a>
- <script>
- functionsendInfoToJava(){
- //调用android程序中的方法,并传递参数
- varname=document.getElementById("name_input").value;
- window.AndroidWebView.showInfoFromJs(name);
- }
- //在android代码中调用此方法
- functionshowInfoFromJava(msg){
- alert("来自客户端的信息:"+msg);
- }
- </script>
- </body>
- </html>
更多相关文章
- Android(安卓)'记住密码'功能
- 【边做项目边学Android】手机安全卫士05_1:程序主界面
- android arm debug
- View常见XML属性及相关方法
- Android中字体加粗
- JS与Android交互之html页面跳转到Android(安卓)Activity
- android中wifi原理及流程分析
- 转:Android(安卓)开发技巧杂集
- Android多媒体应用——ImageSwitcher