Android java与HTML的交互

分析以上实现的功能要点:

  • title和title中的返回按钮是用HTML实现的,点击返回按钮回到上一个activity
  • 输入框HTML按钮都是HTML部分,在输入框中输入文字,然后点击HTML按钮会显示一个Toast,如图(name = 张三丰)
  • 底部的按钮ANDROID按钮会在HTML的输入框中显示张三丰
分析完了看代码:

Android xml布局如下:

<?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/activity_web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="luoxiang.com.webviewh5demo.WebViewActivity">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="android按钮"
        />

</RelativeLayout>
java代码:

package luoxiang.com.webviewh5demo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

public class WebViewActivity extends AppCompatActivity implements View.OnClickListener{

    private WebAppInterface mAppInterface;
    private WebView mWebView;
    private Button mButton;

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

        mAppInterface = new WebAppInterface(this);

        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(this);
        initWV();
    }

    private void initWV() {
        mWebView = (WebView) findViewById(R.id.webView);

        mWebView.loadUrl("file:///android_asset/index.html");//加载本地的html

        WebSettings settings = mWebView.getSettings();//获取WebSettings对象,利用WebSettings配置WebView

        settings.setJavaScriptEnabled(true);//设置允许执行JS脚本

        mWebView.addJavascriptInterface(mAppInterface,"app");//添加HTML与AAndroid的通讯接口

    }

    @Override
    public void onClick(View view) {
       mAppInterface.showName("张三丰");
    }


    class WebAppInterface{
        private Context context;

        public WebAppInterface(Context context) {
            this.context = context;
        }

           下面是JS调用安卓的的方法

        @JavascriptInterface
        public void sayHello(String name){
            Toast.makeText(context,"name="+name,Toast.LENGTH_SHORT).show();
        }
        @JavascriptInterface
        public void back(){
            finish();
        }

        //    下面是安卓调用js的的方法
        @JavascriptInterface
        public void showName(final String name){
           runOnUiThread(new Runnable() {     //Android更新UI需要在主线程
               @Override
               public void run() {
                   mWebView.loadUrl("javascript:showName('"+name+"')");
               }
           });
        }

    }
}

HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">  
	<title>HTML 测试</title>
	
	<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}
		.title{
			position: relative;
			width: 100%;
			height: 50px;
			background-color: #f90;
		}
		.back{
			position: absolute;			
			font-size: 24px;
			background-color:#f92;
			width: 60px;
			height: 50px;
		}
		.title h2{
			position: absolute;
			right: 30%;
			height: 50px;
			float: left;
			line-height: 50px;
		
		}
		.content{
			margin: 3px;
		}
		#textName{
			display: block;
			width: 100%;
			height: 35px;
		}
		.button{
			display: block;
			width: 100%;
			height: 35px;
			font-size: 24px;
			border: none;
			background-color: #f95;
		}

	</style>

<script type="text/javascript">

	function sayHello() {
		var name = document.getElementById('textName').value;
		app.sayHello(name);
	}

	function back(){
		app.back();
	}

	function showName(name){
		document.getElementById('textName').value = name;
	}
	
</script>

</head>
<body>
<div class="title">
	<input class="back" type="button" οnclick="back()" value="返回">
	<h2>HTML5 Test</h2>
</div>
<div class="content">
	<input id="textName" type="text">
	<button class="button" οnclick="sayHello()">HTML按钮</button>
</div>	
</body>
</html>


HTML代码是使用本地的,放在assets目录中。


  • HTML与Android的交互总结为四个步骤:
  1. 配置可执行JS脚本    websettings.setJavaScriptEnabled(true);
  2. 添加通讯接口  webView.addJavaScriptInterface(Interface,"InterfaceName");
  3. js调用Android  InterfaceName.MethodName();
  4. Android调用js webView.loadUrl("javaScript:functionName");







                
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值