Webview中的Android文本选择

时间:2012-05-04 12:21:03

标签: android webview clipboard

我使用webview在Android Honeycomb(3.x)中显示内容。我创建了用于剪切,复制和粘贴的自定义操作菜单。如何使用我自定义的操作菜单在Webview中复制所选文本。

2 个答案:

答案 0 :(得分:8)

愿它有所帮助......

public void selectAndCopyText() {
try {
    Method m = WebView.class.getMethod("emulateShiftHeld", null);
    m.invoke(this, null);
} catch (Exception e) {
    e.printStackTrace();
    // fallback
    KeyEvent shiftPressEvent = new KeyEvent(0,0,
         KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
    shiftPressEvent.dispatch(this);
}

}

来自https://stackoverflow.com/a/1113204/638987

答案 1 :(得分:-2)

尝试以下代码......

private void emulateShiftHeld(WebView view)
{
    try
    {
        KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                                                KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
        shiftPressEvent.dispatch(view);
        Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
    }
    catch (Exception e)
    {
        Log.e("dd", "Exception in emulateShiftHeld()", e);
    }
}

并在任何地方调用上述方法......

emulateShiftHeld(mWebView);

有关详细信息,请参阅此... Android: how to select texts from webview