webview show弹出选项android

时间:2013-12-19 08:32:25

标签: android webview

当用户从webview中选择文本时,我需要提供如下图所示的选项。

enter image description here

我应该覆盖哪种方法来提供选项?

1 个答案:

答案 0 :(得分:0)

Android 3.0及更高版本默认提供WebView中的复制功能。要在低于3.0的版本中启用复制,需要添加一些代码。我做的第一件事就是在视图底部的Actionbar添加一个复制按钮。

查看Copy Text in a WebView in Android

以下是显示如何使用的代码:

public class TryDemoActivity extends Activity implements
OnClickListener {
    private WebView webview1;
    private Button copyButton;
    private Button pasteButton;
    private Button highlightButton;
    private ClipboardManager clipboard;
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview1 = (WebView) findViewById(R.id.webview);
    copyButton = (Button) findViewById(R.id.copy);
    pasteButton = (Button) findViewById(R.id.paste);

    webview1.loadUrl("file:///android_asset/1.1.html");
    clipboard =(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    copyButton.setOnClickListener(this);
    pasteButton.setOnClickListener(this);
}
private void emulateShiftHeld(WebView view)
{
    try
    {
        KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
    shiftPressEvent.dispatch(view);

    registerForContextMenu(view);
    Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();

    }
    catch (Exception e)
    {
        Log.e("dd", "Exception in emulateShiftHeld()", e);
    }
}
@Override
public void onClick(View v) {
    switch(v.getId())
    {
        case R.id.copy:
            emulateShiftHeld(webview1);
        break;
        case R.id.paste:
        Toast.makeText(this, clipboard.getText(),Toast.LENGTH_SHORT).show();
        break;
    }
   }
 }

<强>编辑:

注意:代码 emulateShiftHeld ,在4.0中已弃用,这可能就是它在2.2而不是4.0中的原因。

查看我找到的Post

HERE