webview调用的html文件,需要点击html文件里面的按钮,关闭当前activity。
WebViewActivity.class
public class WebViewActivity extends Activity {
private Context mContext;
private WebView mWebView;
@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
mContext = this;
mWebView = (WebView) findViewById(R.id.myWebView);
mWebView.loadUrl("file:///android_asset/test.html");
WebSettings mSettings = mWebView.getSettings();
mSettings.setJavaScriptEnabled(true);
//响应html页面js代码
mWebView.addJavascriptInterface(new Object() {
@SuppressWarnings("unused")
public void androidOnclick(){
Toast.makeText(mContext, "接收到了", Toast.LENGTH_LONG).show();
finish();
}
}, "app");
}
}
test.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>响应JS</title>
<style type="text/css">
.txt {width: 90%; text-align: center; background: #ff99aa; display: block; padding: 5%;}
</style>
</head>
<body>
<a href="javascript:run();" class="txt">完成</a>
</body>
<script type="text/javascript">
function run() {
window.app.androidOnclick();
}
</script>
</html>