**
* webview设置工具类
* Created by 李森 on 2017/12/27.
*/
public class WebviewUtil {
private static WebSettings webSettings;
static Context con;
static WebView webView;
public static void useWebview(final Context context, final WebView webview){
con=context;
webView=webview;
webSettings = webview.getSettings();
//使用WebSettings进行配置。
//支持缩放
webSettings.setSupportZoom(true);
//设置webView使其支持脚本
webSettings.setJavaScriptEnabled(true);
//关闭WebView中缓存
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
//支持通过js打开新窗口
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
//使其可以自动加载图片
webSettings.setLoadsImagesAutomatically(true);
//启用html5 local storage
webSettings.setDomStorageEnabled(true);
webview.getSettings().setTextZoom(100);
//隐藏掉webView的滚动条
webview.setHorizontalScrollBarEnabled(false);
webview.setVerticalScrollBarEnabled(false);
// webview.setWebViewClient(new WebViewClient() {
// @Override
// public void onPageStarted(WebView view, String url, Bitmap favicon) {
// super.onPageStarted(view, url, favicon);
// DialogUtils.showLoading(context);
// }
//
// @Override
// public void onPageFinished(WebView view, String url) {
// super.onPageFinished(view, url);
// DialogUtils.hideLoading();
// }
// });
// webview.loadUrl(url);
//监听Android返回键,对H5页面的返回
webview.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
handler.sendEmptyMessage(1);
return true;
}
return false;
}
});
}
private static Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case 1: {
webViewGoBack();
}
break;
}
}
};
private static void webViewGoBack() {
webView.goBack();
}
//分割H5的url,以?为分隔符,返回一个Map集合存储拼接在url后的参数,以键值对的形式存储
public static Map<String, String> getParameters(String url) {
Map<String, String> params = new HashMap<String, String>();
if (url == null || "".equals(url.trim())) {
return params;
}
try {
url = URLDecoder.decode(url, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String[] split = url.split("[?]");
if (split.length == 2 && !"".equals(split[1].trim())) {
String[] parameters = split[1].split("&");
if (parameters != null && parameters.length != 0) {
for (int i = 0; i < parameters.length; i++) {
if (parameters[i] != null
&& parameters[i].trim().contains("=")) {
String[] split2 = parameters[i].split("=");
//split2可能为1,可能为2
if (split2.length == 1) {
//有这个参数但是是空的
params.put(split2[0], "");
} else if (split2.length == 2) {
if (!"".equals(split2[0].trim())) {
params.put(split2[0], split2[1]);
}
}
}
}
}
}
return params;
}
}
//调用上面的类
WebviewUtil.useWebview(getContext(), webview);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
DialogUtils.showLoading(context);//开始加载H5页面
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
DialogUtils.hideLoading();//H5页面加载完毕
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Map<String, String> map = WebviewUtil.getParameters(url);
for (String key : map.keySet()) {
//拿到拼接在网址后面的值
price = map.get("price");
}
webview.loadUrl(url);
}
return true;
}
});
webview.loadUrl(要加载的Url);
//弹出框管理DialogUtils的工具类
public class DialogUtils {
/**
* 日期组件
*/
private static DatePickerDialog datePickerDialog = null;
private static ProgressDialog progressDialog = null;
/**
* 展示loading
* @param context
*/
public static void showLoading(Context context){
if (null == progressDialog){
progressDialog = new ProgressDialog(context);
}
progressDialog.setCancelable(false);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("加载中......");
if (null != progressDialog && !progressDialog.isShowing()){
progressDialog.show();
}
}
/**
* 隐藏加载框
*/
public static void hideLoading(){
if (null != progressDialog && progressDialog.isShowing()){
progressDialog.cancel();
progressDialog = null;
}
}
/**
* 展示日期组件(显示在组件上)
*/
public static void showDateDialog(Context context, final TextView textView){
Calendar calendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
textView.setText(year + "-" + (month + 1) + "-" + dayOfMonth);
}
}, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
if (null != datePickerDialog && !datePickerDialog.isShowing()){
datePickerDialog.show();
}
}
/**
* 显示日期组件(回调)
* @param context
* @param iDatePickerDialog
*/
public static void showDateDialog(Context context, final IDatePickerDialog iDatePickerDialog){
if (null != iDatePickerDialog){
Calendar calendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
iDatePickerDialog.showDate(year + "", (month + 1) > 9 ? (month + 1) + "" : "0" + (month + 1),
dayOfMonth > 9 ? dayOfMonth + "" : "0" + dayOfMonth);
}
}, calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
if (null != datePickerDialog && !datePickerDialog.isShowing()){
datePickerDialog.show();
}
}
}
public interface IDatePickerDialog{
void showDate(String year, String month, String day);
}
}