android 调用js,js调用android
Java调用JavaScript
1.main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?xml version= "1.0" encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <TextView android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "Welcome to Mr Wei's Blog." /> <WebView android:id= "@+id/webview" android:layout_width= "fill_parent" android:layout_height= "wrap_content" /> <Button android:id= "@+id/button" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "Change the webview content" /> </LinearLayout> |
2.demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< html > < mce:script language="javascript"> <!-- function fillContent(){ document.getElementById("content").innerHTML = "This Content is showed by Android invoke Javascript function."; } // --> </ mce:script > < body > < p >< a onClick="window.demo.startMap()" href="">Start GoogleMap</ a ></ p > < p id="content"></ p > < p >A Demo ----Android and Javascript invoke each other.</ p > < p >Author:Frankiewei</ p > </ body > </ html > |
3.WebViewDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
|
package com.tutor.webwiewdemo; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Button; public class WebViewDemo extends Activity { private WebView mWebView; private Button mButton; public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } //初始化 private void setupViews() { mWebView = (WebView) findViewById(R.id.webview); WebSettings mWebSettings = mWebView.getSettings(); //加上这句话才能使用javascript方法 mWebSettings.setJavaScriptEnabled( true ); //增加接口方法,让html页面调用 mWebView.addJavascriptInterface( new Object() { //这里我定义了一个打开地图应用的方法 public void startMap() { Intent mIntent = new Intent(); ComponentName component = new ComponentName( "com.google.android.apps.maps" , "com.google.android.maps.MapsActivity" ); mIntent.setComponent(component); startActivity(mIntent); } }, "demo" ); //加载页面 mWebView.loadUrl( "file:///android_asset/demo.html" ); mButton = (Button) findViewById(R.id.button); //给button添加事件响应,执行JavaScript的fillContent()方法 mButton.setOnClickListener( new Button.OnClickListener() { public void onClick(View v) { mWebView.loadUrl( "javascript:fillContent()" ); } }); } } |
首界面 点击按钮时,html内容改变
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.example.jsdemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.webkit.WebSettings; import android.webkit.WebView; public class MainActivity extends AppCompatActivity { private WebView wView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); wView = (WebView) findViewById(R.id.wView); wView.loadUrl( "file:///android_asset/demo1.html" ); WebSettings webSettings = wView.getSettings(); //①设置WebView允许调用js webSettings.setJavaScriptEnabled( true ); webSettings.setDefaultTextEncodingName( "UTF-8" ); //②设置支持js调用java wView.addJavascriptInterface( new AndroidAndJSInterface(), "Android" "); } class AndroidAndJSInterface{ @JavascriptInterface public void showToast(){ Toast.makeText(MainActivity. this , "我被js调用了" , Toast.LENGTH_SHORT).show(); } } } |
注意:解决该WebView.addJavascriptInterface接口不起作用的两种办法
①针对版本改成16
②在JavaScript接口类的方法加上@JavascriptInterface注解
2.JavaScript调用Java对象示例
demo1.html
1
|
<input type= "button" value= "点击Android被调用" onclick= "window.Android.showToast()" /> |
android 调用js,js调用android的更多相关文章
- [转]JS调用Android里面的方法,Android调用JS里面的方法
FROM : http://blog.csdn.net/hj563308597/article/details/45197709 Android WebView 在公司Android的开发过程中遇到一 ...
- Android开发学习之路--Java和Js互相调用
随着前端的火热,以前开发的快速,越来越多的native app在其中融合了h5,就拿淘宝就是很多的h5组成的,一旦出现什么节日,他都可以不用通过更新app来实现界面的改变,而且android和io ...
- Android与js互相调用
有话要说: 本篇主要总结了简单的Android与js互相调用的方法. 在开发过程中遇到了需要在安卓中调用js方法的需求,于是将具体的实现过程总结成这篇博客. 效果: 其中“调用安卓方法”按钮是html ...
- iOS 或者Android调用vue.js 里面的方法
1.原生调用vue.js 某个vue组件下的方法. 比如**.vue里面有个这样的方法: 如果这样的话,在iOS或者Android里面是调用不了这个ajax方法的. 需要在**.vue (我的版本是v ...
- android js 互相调用
代码地址如下:http://www.demodashi.com/demo/13107.html android js 互相调用 第二版 支持js匿名函数接收 支持js json对象接收 支持js函数返 ...
- 【Android】java中调用JS的方法
最近因为学校换了新的教务系统,想做一个模拟登陆功能,发现登陆的账号和密码有一个js脚本来进行加密 整理了一下java中执行JS的方法 智强教务 账号 密码 加密方法 var keyStr = &quo ...
- Android和JavaScript相互调用的方法
转载地址:http://www.jb51.net/article/77206.htm 这篇文章主要介绍了Android和JavaScript相互调用的方法,实例分析了Android的WebView执行 ...
- Android-webview和js互相调用
Android-webview和js互相调用 Android 和 H5 都是移动开发应用的非常广泛.市面上很多App都是使用Android开发的,但使用Android来开发一些比较复杂附属类,提示性的 ...
- WebView使用详解(一)——Native与JS相互调用(附JadX反编译)
念念不忘,必有回响,永远坚持你所坚持的! 一直在用WebView,还没有系统的总结过它的用法,下面就系统的总结下,分享给大家 一.基本用法 1.加载在线URL void loadUrl(String ...
- Hybrid App开发模式中, IOS/Android 和 JavaScript相互调用方式
IOS:Objective-C 和 JavaScript 的相互调用 iOS7以前,iOS SDK 并没有原生提供 js 调用 native 代码的 API.但是 UIWebView 的一个 dele ...
随机推荐
- 【非原创】codeforces 1060E Sergey and Subway 【树上任意两点距离和】
学习博客:戳这里 本人代码: 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 con ...
- Java之一个整数的二进制中1的个数
这是今年某公司的面试题: 一般思路是:把整数n转换成二进制字符数组,然后一个一个数: private static int helper1(int i) { char[] chs = Integer. ...
- hdu 5883
Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM ...
- python xml转excle
<?xml version="1.0" encoding="UTF-8"?> <RECORDS xmlns:xsi="http:// ...
- free video tutorial of Deep Learning
free video tutorial of Deep Learning AI 深度学习/ 机器学习/人工智能 Deep Learning With Deep Learning – a form of ...
- HTML5 Animation Creator | Hype 官方文档
HTML5 Animation Creator | Hype 官方文档 HTML5 Animation Build 7个 专业术语 场景,元素,属性,关键帧,动画,时间线,操作 Hype 官方文档 h ...
- React Hooks: useState All In One
React Hooks: useState All In One useState import React, { useState } from 'react'; function Example( ...
- 钉钉 & URL Scheme & Universal Link & Deep Link
钉钉 & URL Scheme & Universal Link & Deep Link DD link https://www.cnblogs.com/xgqfrms/p/1 ...
- linux bash which
linux bash which https://linuxize.com/post/linux-which-command/ Linux which command is used to ident ...
- cURL all in one
cURL all in one convert http request to curl online https://curlbuilder.com/ https://cdn.xgqfrms.xyz ...