WebView简单使用
public class MainActivity extends Activity {
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
//允许使用js
webView.getSettings().setJavaScriptEnabled(true);
//JS映射
webView.addJavascriptInterface(new WebHost(this), "js");
//WebViewClient主要帮助WebView处理各种通知、请求事件的
//WebChromeClient主要辅助WebView处理JavaScript的对话框、网站图标、网站title、加载进度等
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//显示本地静态页
webView.loadUrl("file:///android_asset/login.html");
}
public class WebHost {
public Context context;
public WebHost(Context context){
this.context = context;
}
@JavascriptInterface
public void call(){
Toast.makeText(context, "点击了登录按钮!", Toast.LENGTH_SHORT).show();
}
}
}