将文本粘贴到webview中

时间:2011-08-16 01:29:42

标签: android

在默认的Android浏览器中,当您长按网页内的文本框时,您会看到一个上下文菜单,其中显示了几个选项,例如将文本粘贴到文本框中的功能。如何使用自己的webview复制此功能?

我查看了android源代码,特别是处理上下文菜单创建的代码,发现了这个:

     @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

         ...

         WebView.HitTestResult result = webview.getHitTestResult();
         int type = result.getType();
         if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {
             // let TextView handles context menu
             return;
         }

         ...

    }

代码的含义是当用户长按“EDIT_TEXT_TYPE”即。一个文本框,webview什么都不做。一些神奇的“TextView”处理上下文菜单。现在我迷路了,如何让这个上下文菜单显示在我的网页浏览中?

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用registerForContextMenu()。我知道它确实适用于ListView,但它也适用于其他视图。

它应该是这样的:

registerForContextMenu(yourWebView);

@Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

       //Code for "Paste";

     }

希望这有帮助!

相关问题