2018-01-29

2018-01-29  本文已影响0人  温兴婷

Android中js和java互调传值

通过调用js计算两个数的和,js调用Android的hello方法

testjs.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>JavaAndScript</title>
        <script>
         //js调用android的hello方法
        function jscallandroid(){
                test.hello("js调到了Android的hello方法");
            }
        //Android要调用的方法,计算a和b的和并且返回
        function androidcalljs(a,b){
        return a+b;
        }
        </script>
    </head>
    <body>
        <button style="color: #FF0000;" onclick="jscallandroid()">网页里的按钮</button>
        <p id="p1" ></p>
    </body>
</html>

JavaAndJSActivity.java

package com.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;

public class JavaAndJSActivity extends Activity {

   WebView webView;
    Button btn;
    EditText editText1,editText2;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_java_and_js);
        webView = (WebView)findViewById(R.id.webview1);
        btn = (Button)findViewById(R.id.btn1);
        editText1 =(EditText)findViewById(R.id.et_1);
        editText2 = (EditText)findViewById(R.id.et_2);
        textView = (TextView)findViewById(R.id.tv1);
        initwebview();
        btnonclick();
    }
    private void initwebview(){
        WebSettings webSettings = webView.getSettings();
        //设置允许与js交互
        webSettings.setJavaScriptEnabled(true);
        // 先载入JS代码
        // 格式规定为:file:///android_asset/文件名.html
        webView.loadUrl("file:///android_asset/testjs.html");
        // 通过addJavascriptInterface()将Java对象映射到JS对象
        //参数1:Javascript对象名
        //参数2:Java对象名
        webView.addJavascriptInterface(new AndroidtoJs(),"test");

    }
    private void btnonclick(){
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        //点击btn调用js里的androidcalljs()方法,注意方法名要对上
                        webView.evaluateJavascript("javascript:androidcalljs("+editText1.getText().toString()+","+editText2.getText().toString()+")", new ValueCallback<String>() {
                            @Override
                            public void onReceiveValue(String value) {
                                //此处为 js 返回的结果
                                textView.setText(value);
                            }
                });
            }
        });
    }


    public class AndroidtoJs{
        @JavascriptInterface
        public String hello(final String a ){
            Toast.makeText(getApplication(),a,Toast.LENGTH_SHORT).show();
            return textView.getText().toString();
        }
    }
}

运行效果

3.png 1.png 2.png
上一篇下一篇

猜你喜欢

热点阅读