java和js互调 webview
Posted znsongshu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java和js互调 webview相关的知识,希望对你有一定的参考价值。
public class JavaAndJSActivity extends Activity implements View.OnClickListener {
private EditText etNumber;
private EditText etPassword;
private Button btnLogin;
/**
* 加载网页或者说H5页面
*/
private WebView webView;
/**
* Find the Views in the layout<br />
* <br />
* Auto-created on 2016-07-28 11:43:37 by android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
private void findViews() {
setContentView(R.layout.activity_java_and_js);
etNumber = (EditText) findViewById(R.id.et_number);
etPassword = (EditText) findViewById(R.id.et_password);
btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(this);
initWebView();
}
/**
* Handle button click events<br />
* <br />
* Auto-created on 2016-07-28 11:43:37 by Android Layout Finder
* (http://www.buzzingandroid.com/tools/android-layout-finder)
*/
@Override
public void onClick(View v) {
if (v == btnLogin) {
// Handle clicks for btnLogin
login();
}
}
private void login() {
String numebr = etNumber.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if (TextUtils.isEmpty(numebr) || TextUtils.isEmpty(password)) {
Toast.makeText(JavaAndJSActivity.this, "账号或者密码为空", Toast.LENGTH_SHORT).show();
} else {
//登录
login(numebr);
}
}
/**
* Java调用javascript
*
* @param numebr
*/
private void login(String numebr) {
webView.loadUrl("javascript:javaCallJs(" + "‘" + numebr + "‘" + ")");
setContentView(webView);
}
private void initWebView() {
webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
//设置支持javaScript脚步语言
webSettings.setJavaScriptEnabled(true);
//支持双击-前提是页面要支持才显示
// webSettings.setUseWideViewPort(true);
//支持缩放按钮-前提是页面要支持才显示
webSettings.setBuiltInZoomControls(true);
//设置客户端-不跳转到默认浏览器中
webView.setWebViewClient(new WebViewClient());
//设置支持js调用java
webView.addJavascriptInterface(new AndroidAndJSInterface(),"Android");
//加载网络资源
// webView.loadUrl("http://10.0.2.2:8080/assets/JavaAndJavaScriptCall.html");
//加载assets文件夹下的资源
webView.loadUrl("file:///android_asset/JavaAndJavaScriptCall.html");
//显示页面
// setContentView(webView);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
findViews();
}
/**
* js可以调用该类的方法
*/
class AndroidAndJSInterface{
@JavascriptInterface
public void showToast(){
Toast.makeText(JavaAndJSActivity.this, "我被js调用了", Toast.LENGTH_SHORT).show();
}
}
}
JavaAndJavaScriptCall.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<script type="text/javascript">
function javaCallJs(){
document.getElementById("content").innerHTML +=
"<br>java调用了js无参函数";
}
function javaCallJs(arg){
document.getElementById("content").innerHTML =
("欢迎:"+arg );
}
function showDialog(){
alert("你好,我是来自javascript");
}
</script>
</head>
<body>
<div align="left" id="content"> 555</div>
<div align="right">光临</div>
<p><img src="http://atguigu.com/images/logo.gif"></p>
<input type="button" value="点击Android被调用" onclick="window.Android.showToast()" />
</body>
</html>
以上是关于java和js互调 webview的主要内容,如果未能解决你的问题,请参考以下文章