码迷,mamicode.com
首页 > 移动开发 > 详细

Android与H5交互(java与js的交互)

时间:2016-04-16 09:30:36      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

1.Android  java代码 

//java代码

package com.wytiger.jsdemo;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.JsResult;
import android.widget.Button;
import android.widget.Toast;

public class JsActivity1 extends Activity {

private Button mBtn;
private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_js1);

// 初始化
initViews();

initWebView();
}

private void initWebView() {
// 设置编码
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
// 设置背景颜色 透明
mWebView.setBackgroundColor(Color.argb(0, 0, 0, 0));
// 如果不设置这个,JS代码中的按钮会显示,但是按下去却不弹出对话框
mWebView.setWebChromeClient(new WebChromeClient() {

@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
return super.onJsAlert(view, url, message, result);
}

});

// 支持java调用js
mWebView.getSettings().setJavaScriptEnabled(true);
// 载入html页面
mWebView.loadUrl("file:///android_asset/js_test.html");
// 设置本地调用对象及对象名
//js使用此对象名调用java本地方法
AndroidObj javaObj = new AndroidObj(this);
mWebView.addJavascriptInterface(javaObj, "javaObj");
}

private void initViews() {
mBtn = (Button) findViewById(R.id.btn);
mWebView = (WebView) findViewById(R.id.webView);

//java调用js中方法
mBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// 调用js中方法
mWebView.loadUrl("javascript:callJs()");
Toast.makeText(JsActivity1.this, "调用js的callJs()方法",
Toast.LENGTH_LONG).show();
}
});

}

public class AndroidObj {
Context mContxt;

public AndroidObj(Context mContxt) {
this.mContxt = mContxt;
}

// api17版本以上加上注解
@JavascriptInterface
public void callJava(String name) {
Toast.makeText(mContxt, name, Toast.LENGTH_LONG).show();
}

public void callJava2(String name) {
Toast.makeText(mContxt, "调用callJava2方法:" + name, Toast.LENGTH_SHORT)
.show();
}
}

}

//布局文件

<RelativeLayout xmlns: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"
tools:context="com.wytiger.jsdemo.MainActivity" >

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="java调用js方法" />


<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/btn"
android:background="#ff0000" />

<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btn"
android:layout_marginTop="10dp" />

</RelativeLayout>

 

2.H5 js代码,将此html文件放入assets文件夹即可

<html>
<title>测试Android Java与H5 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>这是h5页面</p>
<a class = "rect" onclick="sendInfoToJava()">java中调用js方法</a>
<script>
function callJs(){
document.getElementById("helloweb").innerHTML="Hello,i‘m js";
}

var aTag = document.getElementsByTagName(‘a‘)[0];
aTag.addEventListener(‘click‘, function(){
//H5 js调用Android java本地方法
javaObj.callJava("js调用java方法callJava(String name)!!");
return false;
}, false);
</script>

<div id="helloWeb"></div>
</body>

</html>

 

 

最后,推荐一个开源库:JsBridge

Android与H5交互(java与js的交互)

标签:

原文地址:http://www.cnblogs.com/wytiger/p/5397662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!