一些普通的设置
//需要网络权限
webView = (WebView) findViewById(R.id.webView);
WebSettings settings = null;
if (webView != null) {
settings = webView.getSettings();
}
settings.setJavaScriptEnabled(true);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
//自己处理所有的网络请求,而不是打开浏览器
WebViewClient client = new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
};
webView.setWebViewClient(client);
webView.loadUrl("https://www.baidu.com/");
//使webView监听返回键,能够进行网页的返回
webView.setOnKeyListener(
new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
}
);