android加载H5界面部分按钮不能触发点击事件及展示不全问题

最近遇到部分H5界面部分不能点击及有些展示出来的H5界面不全,通过客服反馈过来的问题,大部分都是android4.0以上,android6.0以下的系统出的问题,而6.0以上的手机都正常,这是因为前端H5部分代码不支持手机低版本,那么App端就应该想办法去解决这个问题。

网上说这个原因是:没有启动webview的html5的本地存储功能(不使用HTML5 DOM存储,不会主动加载到Android WebView中)

解决办法:

App端需要在WebView中添加对H5的支持,添加下面一行代码

webView.getSettings().setDomStorageEnabled(true);

这样低版本系统手机就可以正常点击和显示了。

 

https://blog.csdn.net/carson_ho/article/details/52693322  最全面的 Webview 详解

 

清除cookie和缓存的设置:

//清空所有Cookie
CookieSyncManager.createInstance(getContext());  //Create a singleton CookieSyncManager within a context
CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
cookieManager.removeAllCookie();// Removes all cookies.
cookieManager.removeSessionCookie();//移除
CookieSyncManager.getInstance().sync(); // forces sync manager to sync now
//清除网页访问留下的缓存
//由于内核缓存是全局的因此这个方法不仅仅针对webview而是针对整个应用程序.
webView.clearCache(true);
//这个api仅仅清除自动完成填充的表单数据,并不会清除WebView存储到本地的数据
webView.clearFormData();
//清除当前webview访问的历史记录
//只会webview访问历史记录里的所有记录除了当前访问记录
webView.clearHistory();

对webview的其他设置:

private void initwebView() {
    /**设置可以访问文件*/
    webView.getSettings().setAllowFileAccess(true);
    /**如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript*/
    webView.getSettings().setJavaScriptEnabled(true);

    /**设置适应Html5的一些方法*/
    webView.getSettings().setDomStorageEnabled(true);
    /**控制大小--设置缓存最大容量,默认为Max Integer*/
    webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);// 实现8倍缓存
    /**支持通过JS打开新窗口*/
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    /**H5缓存*/
    webView.getSettings().setAppCacheEnabled(true);
    String appCachePath = getApplication().getCacheDir().getAbsolutePath();
    webView.getSettings().setAppCachePath(appCachePath);
    webView.getSettings().setDatabaseEnabled(true);
    /**缩放操作*/
    webView.setInitialScale(150);// 初始显示比例100%
    webView.getSettings().setSupportZoom(true);//支持缩放,默认为true。是下面那个的前提
    webView.getSettings().setBuiltInZoomControls(true);//设置内置的缩放控件。若为false,则该WebView不可缩放
    /**设置自适应屏幕,两者合用*/
    webView.getSettings().setUseWideViewPort(true);//将图片调整到适合webview的大小--关键点
    webView.getSettings().setLoadWithOverviewMode(true); // 缩放至屏幕的大小
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    } else {
        // IMPORTANT!! this method is no longer available since Android 4.3
        // so the code doesn't compile anymore
        // webView.getSettings().setPluginsEnabled(true);
    }
    if (Build.VERSION.SDK_INT >= 11) {
        /**缩放操作*/
        webView.getSettings().setDisplayZoomControls(false);//隐藏原生的缩放控件
    }
    /**
     * 启用mixed content    android 5.0以上默认不支持Mixed Content
     *
     * 5.0以上允许加载http和https混合的页面(5.0以下默认允许,5.0+默认禁止)
     * */
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    }

    webView.setWebViewClient(new CustomWebViewClient());

    webView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onShowCustomView(View arg0, CustomViewCallback arg1) {
            super.onShowCustomView(arg0, arg1);
        }

        @Override
        public void onShowCustomView(View arg0, int arg1,
                                     CustomViewCallback arg2) {
            super.onShowCustomView(arg0, arg1, arg2);
        }

        @Override
        public void onProgressChanged(WebView view, int progress) {

        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
            openFileChooserImpl(uploadMsg);
        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback uploadMsg,
                                    String acceptType) {
            openFileChooserImpl(uploadMsg);
        }

        // For Android 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg,
                                    String acceptType, String capture) {
            openFileChooserImpl(uploadMsg);
        }

        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
            openFileChooserImplForAndroid5(filePathCallback);
            return true;
        }

    });

    webView.requestFocus();
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值