在 WebView 中启用 longClick [英] Enable longClick in WebView

查看:21
本文介绍了在 WebView 中启用 longClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器中,您可以长按网址.在我的 WebView 中,您不能.我怎样才能让你做到?

In the browser, you can longClick on URLs. In my WebView, you cannot. How can I make it so you can?

推荐答案

我遇到了同样的问题.

不幸的是,我找不到让标准浏览器菜单选项出现的方法.你必须自己实现每一个.我所做的是使用 activity.registerForContextMenu(webView) 为上下文菜单注册 WebView.然后我子类化了 WebView 并覆盖了这个方法:

Unfortunately, I could not find a way to make the standard browser menu options appear. You have to implement each one yourself. What I did was to register the WebView for context menus with activity.registerForContextMenu(webView). Then I subclassed the WebView and overrode this method:

@Override
protected void onCreateContextMenu(ContextMenu menu) {
    super.onCreateContextMenu(menu);

    HitTestResult result = getHitTestResult();

    MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
                // do the menu action
                return true;
        }
    };

    if (result.getType() == HitTestResult.IMAGE_TYPE ||
            result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
        // Menu options for an image.
        //set the header title to the image url
        menu.setHeaderTitle(result.getExtra());
        menu.add(0, ID_SAVEIMAGE, 0, "Save Image").setOnMenuItemClickListener(handler);
        menu.add(0, ID_VIEWIMAGE, 0, "View Image").setOnMenuItemClickListener(handler);
    } else if (result.getType() == HitTestResult.ANCHOR_TYPE ||
            result.getType() == HitTestResult.SRC_ANCHOR_TYPE) {
        // Menu options for a hyperlink.
        //set the header title to the link url
        menu.setHeaderTitle(result.getExtra());
        menu.add(0, ID_SAVELINK, 0, "Save Link").setOnMenuItemClickListener(handler);
        menu.add(0, ID_SHARELINK, 0, "Share Link").setOnMenuItemClickListener(handler);
    }
}

如果您想执行上下文菜单以外的其他操作,请使用 OnLongClickListener.不管你想拦截长按事件,HitTestResult 是关键.这将使您能够弄清楚用户点击了什么并对其进行处理.

If you want to do something other than a context menu, then use an OnLongClickListener. However you want to intercept the long click event, the HitTestResult is the key. That's what will allow you to figure out what the user clicked on and do something with it.

我自己并没有真正实现保存链接",我只是将其作为示例包含在这里.但是要这样做,您必须自己进行所有处理;您必须发出 HTTP GET 请求,接收响应,然后将其存储在用户 SD 卡上的某个位置.据我所知,无法直接调用浏览器应用程序的下载活动.您的保存链接"代码如下所示:

I haven't actually implemented "Save Link" myself, I just included it as an example here. But to do so you would have to do all the processing yourself; you'd have to make an HTTP GET request, receive the response, and then store it somewhere on the user's SD card. There is no way that I know of to directly invoke the Browser app's download activity. Your "Save Link" code will look something like this:

HitTestResult result = getHitTestResult();
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(result.getExtra());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
    URL url = new URL(result.getExtra());

    //Grabs the file part of the URL string
    String fileName = url.getFile();

    //Make sure we are grabbing just the filename
    int index = fileName.lastIndexOf("/");
    if(index >= 0)
            fileName = fileName.substring(index);

    //Create a temporary file
    File tempFile = new File(Environment.getExternalStorageDirectory(), fileName);
    if(!tempFile.exists())
            tempFile.createNewFile();

    InputStream instream = entity.getContent();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    //Read bytes into the buffer
    ByteArrayBuffer buffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = bufferedInputStream.read()) != -1) {
            buffer.append((byte) current);
    }

    //Write the buffer to the file
    FileOutputStream stream = new FileOutputStream(tempFile);
    stream.write(buffer.toByteArray());
    stream.close();
}

这篇关于在 WebView 中启用 longClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆