美文网首页Android开发
javascript调用java

javascript调用java

作者: 6f9992073cea | 来源:发表于2017-12-21 15:53 被阅读0次

对于在webview中点击其按钮,响应点击事件的问题:

html文件:

function javacalljs(){

document.getElementById("content").innerHTML +=

"java调用了js函数";

}

function javacalljswithargs(arg){

document.getElementById("content").innerHTML +=

(""+arg);

}

this is my html

点击调用java代码

点击调用java代码并传递参数

内容显示

java代码:

packagecom.myjs_demo_second;

importandroid.annotation.SuppressLint;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.util.Log;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.webkit.JavascriptInterface;

importandroid.webkit.WebView;

importandroid.widget.Button;

importandroid.widget.TextView;

importandroid.widget.Toast;

public classMainActivityextendsActivity {

privateWebViewcontentWebView=null;

privateTextViewmsgView=null;

//    @SuppressLint("SetJavaScriptEnabled")

@SuppressLint("JavascriptInterface")

@Override

public voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

contentWebView= (WebView) findViewById(R.id.webview);

msgView= (TextView) findViewById(R.id.msg);

// 启用javascript

contentWebView.getSettings().setJavaScriptEnabled(true);

contentWebView.addJavascriptInterface(newMyJSOnList(),"wst");

// 从assets目录下面的加载html

contentWebView.loadUrl("file:///android_asset/wst.html");

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(btnClickListener);

//        contentWebView.addJavascriptInterface(this, "wst");

}

OnClickListenerbtnClickListener=newButton.OnClickListener() {

public voidonClick(View v) {

// 无参数调用

contentWebView.loadUrl("javascript:javacalljs()");

// 传递参数调用

contentWebView.loadUrl("javascript:javacalljswithargs("+"'hello world'"+")");

}

};

public classMyJSOnList {

@JavascriptInterface

public voidstartFunction() {

Log.e("TAG","/////JS调用。。。。startFunction");

Toast.makeText(MainActivity.this,"js调用了java函数",Toast.LENGTH_SHORT).show();

runOnUiThread(newRunnable() {

@Override

public voidrun() {

msgView.setText(msgView.getText() +"\njs调用了java函数");

}

});

}

@JavascriptInterface

public voidstartFunction(finalString str) {

Log.e("TAG","/////JS调用。。String。。startFunction");

Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();

runOnUiThread(newRunnable() {

@Override

public voidrun() {

msgView.setText(msgView.getText() +"\njs调用了java函数传递参数:"+str);

}

});

}

}

}

在assets文件中有个本地的wst.html文件(可以用浏览器打开,安卓中用webview显示)

注意:

html的点击事件:

点击调用java代码

其中wst是js对象名;startFunction,是java代码的点击事件的方法,二者需要一一对应。

java:

contentWebView.addJavascriptInterface(new MyJSOnList(), "wst");

1)、添加js交互接口类MyJSOnList,js对象名

2)、其方法startFunction,必须得有@JavascriptInterface

public class MyJSOnList {

@JavascriptInterface

public void startFunction() {

Log.e("TAG", "/////JS调用。。。。startFunction");

Toast.makeText(MainActivity.this, "js调用了java函数", Toast.LENGTH_SHORT).show();

runOnUiThread(new Runnable() {

@Override

public void run() {

msgView.setText(msgView.getText() + "\njs调用了java函数");

}

});

}

}

相关文章

网友评论

    本文标题:javascript调用java

    本文链接:https://www.haomeiwen.com/subject/fetfettx.html