package com.ada56.apps.taxi.ui.login;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.ada56.apps.taxi.passenger.R;
public class JSAndroidActivity extends Activity {
private WebView myWebView = null;
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jsdemo);
myWebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = myWebView.getSettings();// 得到设置属性的对象
webSettings.setJavaScriptEnabled(true);// 使能JavaScript
webSettings.setDefaultTextEncodingName("GBK");//支持中文,否则页面中中文显示乱码
// 限制在WebView中打开网页,而不用默认浏览器
myWebView.setWebViewClient(new WebViewClient());
// 如果不设置这个,JS代码中的按钮会显示,但是按下去却不弹出对话框
// Sets the chrome handler. This is an implementation of WebChromeClient
// for use in handling JavaScript dialogs, favicons, titles, and the
// progress. This will replace the current handler.
myWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
// 用JavaScript调用Android函数:
// 先建立桥梁类,将要调用的Android代码写入桥梁类的public函数
// 绑定桥梁类和WebView中运行的JavaScript代码
// 将一个对象起一个别名传入,在JS代码中用这个别名代替这个对象,可以调用这个对象的一些方法
//myWebView.addJavascriptInterface(new WebAppInterface(this), "myInterfaceName");
myWebView.addJavascriptInterface(new WebViewNative(this), "Native");
//myWebView.loadUrl("file:///android_asset/sample.html");// 载入页面:本地html资源文件
myWebView.loadUrl("http://192.168.211.61:8080/taxi/jsAndroid.html");
}
public void callJSMethod(View view){
//用Android代码调用JavaScript函数:
myWebView.loadUrl("javascript:myFunction('<<callJSMethod>>')");
//这里实现的效果和在网页中点击第一个按钮的效果一致
}
/**
* 自定义的Android代码和JavaScript代码之间的桥梁类
*/
public class WebViewNative {
Context mContext;
/** Instantiate the interface and set the context */
WebViewNative(Context c) {
mContext = c;
}
/** Show a toast from the web page */
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();
}
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myRelativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="40dp"
android:text="@string/hello_world"
tools:context=".WebJSActivity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView1"
android:onClick="callJSMethod"
android:text="android_Button" />
<WebView
android:id="@+id/myWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/textView1" />
</RelativeLayout>
===========================================================================================================================
<html>
<head>
<meta charset="UTF-8">
<!-- JavaScript脚本,主要包括了按钮要执行的函数,显示对话框等 -->
<script type="text/javascript">
//JavaScript方法,弹出对话框显示信息
function myFunction(msg){
alert("myFunction is called, msg="+msg);
}
function onAlert()
{
console.log("onAlert method");//显示调试信息
alert("This is a alert sample from html");
}
function onConfirm()
{
console.log("onConfirm method");
var b = confirm("are you sure to login?");
alert("your choice is " + b);
}
function onPrompt()
{
console.log("onPrompt method");
var b = prompt("please input your password", "aaa");
alert("your input is " + b);
}
//调用绑定的Java对象的方法,即调用Android代码显示对话框
function showAndroidToast(toast)
{
console.log("showAndroidToast method");
//myInterfaceName.showToast(toast);//注意此处的myInterfaceName要和外部传入的名字一致,大小写正确
Native.showToast(toast);
}
</script>
</head>
<body>
<p>
<!-- 前四个按钮调用JS函数 -->
JavaScript函数调用 <br />
<button onclick="myFunction()">点击这里!</button>
<br />
<input type="button" value="alert" onclick="onAlert()" /> <br />
<input type="button" value="confirm" onclick="onConfirm()" /> <br />
<input type="button" value="prompt" onclick="onPrompt()" /><br />
<!-- 上面用了两种定义按钮的方式,效果一样的 -->
</p>
<p>
<!-- 这个Say hello 按钮调用Android代码中的方法 -->
用JavaScript按钮调用Android代码 <br />
<input type="button"
value="Say hello" onClick="showAndroidToast('Hello Android!')" />
</p>
<a href="http://www.google.com" />Google
</a>
</body>
</html>
相关推荐
uniApp或者vue 与 Android或者ios实现原生交互,使用bridge.js可以轻松实现。
android原生和js交互传递参数 包含特殊字符 例如"\'"等情况 避免Uncaught SyntaxError: missing ) after argument list 避免JSON.parse失败等情况 利于快速和webview交互 加速开发过程
android上很不错的js与原生的交互第三方库 方便用户适配android各系统
android原生与js互调 android原生与js互调 android原生与js互调
用JS实现android原生与html之间的交互
EasyJSWebViewSample 仿android 原生js交互的框架
Android与JS的交互的具体讲解请参考 1、Android与JS的交互(上) 链接:http://blog.csdn.net/u010857510/article/details/52761602 2、Android与JS的交互(下) 链接:...
webView与javascript进行交互
做过 Android 混合开发的,应该都知道 Android 中 Java 代码和 Javascript 代码之间的交互怎么做。 首先回顾一下 Java 和 Javascript 之间的交互。 JavaScript 调用 Java Android 中 WebView 添加供 Html 页面调用的...
JsAndroid Js与原生安卓进行交互案列
vue和原生(ios和android)的交互封装
原生Android 和 js 进行直接交互,不使用插件,欢迎大家下载和进行交流。
实现了android原生代码与html页面进行js交互,能够获取html页面中的输入值,并且实现了点击html页面中的按钮,来调用android中的方法进行与服务器之间的数据交互
Android 原生与JS交互,相互传递资源等。代码展示。少量来源于网络。可用
混合式开发中,我们使用原生安卓与js交互,看这篇看这个demo就够了,注释详细,欢迎欢迎看看博客,互相学习,http://blog.csdn.net/u013519290/article/details/69257536
phonegap开发中JS与Android之间的交互
该项目为ReactNative安卓开发案例,源码中具体实现了ReactNative的JS部分与安卓原生之间的交互包括页面跳转和传值,还实现了ReactNative的JS与JS之间的页面跳转和传值,还实现了常用按钮的样式,看完后让大家明白...
vue前端和安卓原生交互传递数据,js和原生webview交互
首先,由RN中文网关于原生模块(Android)的介绍可以看到,RN前端与原 生模块之间通信,主要有三种方法: (1)使用回调函数Callback,它提供了一个函数来把返回值传回JavaScript。 (2)使用Promise来实现。 (3)...
**Android项目创建UI的新思路:用JavaScript与Activity进行交互** 本资源介绍了一种在Android项目中创新性地使用JavaScript与Activity进行交互的方法。这种方法打破了传统的Android UI设计模式,提供了一种更加灵活...