开发者

How to enable the default highlight menus in android webview?

How to enable the default text highlights menu like:开发者_如何学编程 Copy/Paste/Search/Share in android webview ?

How to enable the default highlight menus in android webview?


Working on Android 1.5 - 2.3 you can use emulateShiftHeld() made public since 2.2 but now is deprecated. this method put your WebView into text selection mode.

https://developer.android.com/reference/android/webkit/WebView.html#emulateShiftHeld%28%29

Unfortunately there's no copy/paste/search/share function integrated in Android, since Android 2.0 the text selection can be driven by touch but other than that, there's no other thing you can do.


I found a workaround for this Check out method selectText() on WebView (it's not in API, but can be invoked using reflection)

here is my full method source code:

 public void startTextSelection() {
        try {
            WebView.class.getMethod("selectText").invoke(this);
        } catch (Exception e) {
            try {
                WebView.class.getMethod("emulateShiftHeld").invoke(this);
            } catch (Exception e1) {
                KeyEvent shiftPressEvent = new KeyEvent(0, 0,
                        KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
                shiftPressEvent.dispatch(this);
                Toast.makeText(getContext(), R.string.select_text, Toast.LENGTH_LONG).show();
            }
        }
    }

Works on ICS too.


Try this:

 mWebView.setHapticFeedbackEnabled(true);
 mWebView.setLongClickable(true);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜