//控件
kweb = view.findViewById(R.id.web);
//adnroid调用js方法
WebSettings webSettings = kweb.getSettings();
//设置支持js
webSettings .setJavaScriptEnabled(true);
kweb.loadUrl(“file:///android_asset/info.html”);
//交互 adnroid调用js方法 //长按事件
kweb.setOnLongClickListener…
//adnroid调用js方法
kweb.loadUrl(“javascript:test1()”);//可以传参数
//1,js调用安卓 协议
kweb.addJavascriptInterface(this, “android”);
@JavascriptInterface
public void jump() {//传参
Toast.makeText(getActivity(), “js调用adnroid方法”, Toast.LENGTH_SHORT).show();
Intent intent =new Intent(getActivity(),jumActivity.class);
startActivity(intent);
}
//页面销毁的时候销毁webView
@Override
public void onDestroy() {
super.onDestroy();
kweb.destroy();
}
…
//设置弹出Alert
kweb2.setWebChromeClient(new WebChromeClient(){
//title就是网页标题
@Override
public void onReceivedTitle(WebView view, String title) {//此方法可获得网页标题
/* Log.i("xxx",view.getTitle());*/
super.onReceivedTitle(view, title);
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
//false弹出 true和super不弹
return false;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result) {
// TODO Auto-generated method stub
return super.onJsPrompt(view, url, message, defaultValue, result);
}
/**处理定位的相关,否则WebView不会开启定位功能,类似百度地图这样的就没法定位*/
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
//这个必须有
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
});
kweb2.setWebViewClient(new WebViewClient(){
//拦截浏览器加载页面,让WebView加载页面
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl("url");
return true;
}
});