Android WebView Javascript getSelection [英] Android WebView Javascript getSelection

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

问题描述

我在使用Android中的WebView进行选择时遇到了一些麻烦。

I am having some trouble getting the selection from a WebView in Android.

我可以让WebView进入选择模式。我甚至可以将它复制到ClipBoard。但我真正想做的是永久地突出选择。

I can get the WebView to go into selection mode. I can even get it to copy the text to the ClipBoard. But what I really want to do is highlight the selection permanently.

所以想法是将WebView置于选择模式。让用户选择文本,然后触发某些内容以突出显示该文本。我可以通过从剪贴板中获取所选文本来使其工作,然后在Javascript中搜索它并突出显示它。当用户选择真正的常用词时会出现问题。我必须要么突出显示它们,要么以某种方式找出选择的位置以获得正确的选择。

So the idea is put the WebView in select mode. Let the user select the text, and then fire something to highlight that text. I can get it to work by getting the selected text from the clipboard, and then search for it in Javascript and highlight it. The problem occurs when the user selects a real common word. I have to either highlight them all or somehow figure out where the selection is to get the right one.

我试过这款适用于iPhone的JavaScript。 Bu getSelection()似乎不适用于Android。

I have tried this JavaScript which works on the iPhone. Bu getSelection() does not seem to work on the Android.

function highlight(colour) {
    var range, sel;
    if (window.getSelection) {
            // Non-IE case
        sel = window.getSelection();
        if (sel.getRangeAt) {
            range = sel.getRangeAt(0);
        }
        document.designMode = "on";
        if (range) {
            sel.removeAllRanges();
            sel.addRange(range);
        }
            // Use HiliteColor since some browsers apply BackColor to the whole block
        if ( !document.execCommand("HiliteColor", false, colour) ) {
            document.execCommand("BackColor", false, colour);
        }
        document.designMode = "off";
    } else if (document.selection && document.selection.createRange) {
            // IE case
        range = document.selection.createRange();
        range.execCommand("BackColor", false, colour);
    }
}

有任何建议吗?

推荐答案

当WebView进入选择模式时,WebView实际上并未被用于选择......它被推送到WebTextView下(私有) Android的武器库中的类,它模仿文本位置,但允许图像显示,并允许您选择出现在实际HTML中的文本。
当您在选择文本后尝试与WebView交互时出现问题。
高亮和光标手柄位于正确的位置,但它们实际上位于我提到的特殊WebTextView中,因此您实际上没有选择通过JavaScript的getSelection或JavaScript中的任何其他方式。
我正在制作ACTION_DOWN(LongPress),它通过JavaScript触发选择以及拖动工作的拖动和ACTION_UP,但它非常多毛,而且此时用户并不友好...

When the WebView goes into "Selection Mode", the WebView is not actually being used for selection... It is being pushed under a "WebTextView" (private class in Android's arsenal) which mimics the text position, yet allows images to show through, and allows you to "select" the text which appears in the actual HTML. The problem comes when you try to interact with the WebView after "selecting" the text. The highlight and cursor handles are in the right position, but they are actually in the special WebTextView I mentioned, therefore you do not actually have a selection to get via JavaScript's getSelection, or any other means in JavaScript. I am working on making the ACTION_DOWN (of the LongPress) which triggers selection and the drag and ACTION_UP of the release from drag work for me via JavaScript, but it is very hairy, and not at all user friendly at this point...

http://www.java2s.com/Open-Source/Android/android-core/platform-frameworks-base/android/webkit/WebTextView.java.htm

检查来源(这是模仿文本选择而不是提供它的大量工作)
这很难过,而且目前非常痛苦我们团队开展的一个项目 - 特别是在为iPad做同样的应用之后......

check the source(that's a lot of work to mimic text selection instead of provide it) It is sad, and currently very painful for a project our team has undertaken -- especially after doing the same app for iPad...

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

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