您的位置:首页 > 编程语言 > Java开发

java调用js

2014-06-07 21:43 88 查看
layout布局文件

<Button
android:layout_weight="1"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="调用html中的js 方法"
/>


java文件【MainActivity.java】

//获取button控件 即"调用html中的js方法" 按钮
Button button=(Button)findViewById(R.id.button);

//给button添加事件响应,执行JavaScript的fillContent()方法
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Log.d("MainActivity","button OnClick");
webview.loadUrl("javascript:updateHtml()");
}


HTML部分

<html>
<meta charset="utf-8">
<head>
//为android提供的js方法
<script type="text/javascript">
function updateHtml(){
document.getElementById("content").innerHTML =
"this is a javascript";
alert("dialog");
}
</script>
</head>
<body>
//为js准备的DOM结构
<span id="content"></span>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: