Android webview HitTestResult识别当前选中的类型

在webview中HitTestResult可以识别当前触碰的地方的类型,可以满足长按文本选中,长按图片保存等功能。

WebView.HitTestResult result = webView.getHitTestResult();

  result.getType() 类型

/** @deprecated */
@Deprecated
public static final int ANCHOR_TYPE = 1;
public static final int EDIT_TEXT_TYPE = 9;
public static final int EMAIL_TYPE = 4;
public static final int GEO_TYPE = 3;
/** @deprecated */
@Deprecated
public static final int IMAGE_ANCHOR_TYPE = 6;
public static final int IMAGE_TYPE = 5;
public static final int PHONE_TYPE = 2;
public static final int SRC_ANCHOR_TYPE = 7;
public static final int SRC_IMAGE_ANCHOR_TYPE = 8;
public static final int UNKNOWN_TYPE = 0;

 result.getExtra() 数据

webView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        final WebView.HitTestResult result = webView.getHitTestResult();
        if (null == result)
            return false;
        int type = result.getType();
        switch (type) {
            case WebView.HitTestResult.EDIT_TEXT_TYPE: // 选中的文字类型
                break;
            case WebView.HitTestResult.PHONE_TYPE: // 处理拨号
                break;
            case WebView.HitTestResult.EMAIL_TYPE: // 处理Email
                break;
            case WebView.HitTestResult.GEO_TYPE: //  地图类型
                break;
            case WebView.HitTestResult.SRC_ANCHOR_TYPE: // 超链接
                break;
            case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE: // 带有链接的图片类型
                break;
            case WebView.HitTestResult.IMAGE_TYPE: // 处理长按图片的菜单项 
                String url = result.getExtra();//获取图片
                break;
            case WebView.HitTestResult.UNKNOWN_TYPE: //未知
                break;
        }
        return false;
    }
});

你可能感兴趣的:(Android)