禁用 Android 中 WebView 的默认高亮选中效果

Android 中嵌入的 WebView 有个默认的橙色高亮选中效果,比较影响用户体验,如果能去掉这个效果,那么用户就几乎完全感受不到和 native 页面的差异了。

首先想到的就是 WebViewsetClickable()setFocusable() 两个方法,发现没有任何效果。然后发现 WebSettings 还有个 setLightTouchEnabled() 方法,试过发现也没有任何效果,官方文档是这么说的:

From JELLY_BEAN this setting is obsolete and has no effect.

说明 4.1 以后这方法就没效果了,而且 API 18 以后这个方法就过时了。

然后发现在手机浏览器打开其他页面也存在这种橙色高亮选中效果,这样看来,应该是 framework 层的 WebKit 做了相关处理。

去修改 framework 层的代码并重新编译显然成本过高,那就只能尝试从前端 Web 页面去解决这个问题。

通过一个 CSS 文件去设置 HTML 页面元素的所有 focusselecttaptouch 相关效果:

-webkit-tap-highlight-color: rgba(255, 255, 255, 0);
-webkit-focus-ring-color: rgba(0, 0, 0, 0);
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

以上代码在 Android 2.3 、4.0.4 、4.1.2 上亲测有效。