java
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
WebView webView = new WebView(this);
// 允许WebView加载JavaScript
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {});
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.addJavascriptInterface(this,"android");
webView.loadUrl("http://192.168.110.88:82/index.html");
setContentView(webView);
}
//打印信息方@Override
@JavascriptInterface
public void print(String msg) {
Toast.makeText( this, "默认Toast样式", Toast.LENGTH_SHORT).show();
System.out.println(msg);
}
}
html
<button id="button">Hello Android!</button>
<script>
document.getElementById('button').onclick = function() {
// 如果nativeMethod对象存在 则调用android里的hello方法
alert("aa");
window.android.print("aaa");
}
</script>
|