禁用PhoneGap中的文本选择

33

是否有可能禁用文本选择功能,使得 PhoneGap 应用程序更类似于正常的本地应用程序?

像这样:

document.onselectstart = function() {return false;}

或者:

* { 
user-select: none;
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
}

或者很多其他事情不起作用。

9个回答

60

对我来说,将其放在html上而不是*上可以正常工作:

html {
    -webkit-user-select: none;
}

这是我需要的修复,使其在WP8上运行。在iOS和Android上没有“html”也可以正常工作。 - ED-209
html { user-select: none;} - math_lab3.ca

32

我寻找了很多帮助,最终这个方法对我有用。

public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        super.loadUrl("file:///android_asset/www/index.html");

        super.appView.setOnLongClickListener(new View.OnLongClickListener() {

            public boolean onLongClick(View v) {
                return true;
            }
        });
    }
}

设置setOnClickListener是实现这个功能的关键。确保在调用super.loadUrl之后添加此代码。

当然,这会禁用整个应用程序的文本选择功能,但目前我可以接受它,并且我没有其他解决方法。

我还不确定这样做的完全影响,但我确实使用JqueryMobile事件“taphold”,它仍然可以正常工作。我相信这是通过处理appView(托管您的HTML应用程序)上的长按事件并防止其冒泡而实现的。


谢谢,它可以工作了!别忘了在loadUrl之后添加它,否则appView可能为空。 - guya
1
对我来说,我必须插入一行代码:super.appView.setLongClickable(false); 然后这个解决方案对我有效。 - Deepika Lalra
你怎么启用文本选择呢?我没有看到WebView的默认行为允许文本选择和一些默认上下文菜单... - android developer
我在 Cordova 中遇到了 Cannot resolve method 'setOnLongClickListener' in 'CordovaWebView' 的问题。 - jinn

9
这也可以正常工作。
<body oncontextmenu="return false" ondragstart="return false" 
onselectstart="return false">

这是目前最优雅的解决方案,适用于Android 6和Cordova 6.5.0。 - andreszs

8

除了ThinkingStiff提到的之外,我也使用以下内容来删除任何高亮/复制&粘贴:

-webkit-touch-callout: none;
-webkit-tap-highlight-color: rgba(0,0,0,0); 

现在没有高亮显示,只有单击后,但如果我按住点,文本选择工具将打开。我该如何阻止它? :/ - stmn

6
添加这个并享受吧。在iOS上也适用。
<style type="text/css">
*:not(input,textarea) {
    -webkit-touch-callout: none;
    -webkit-user-select: none; /* Disable selection/Copy of UIWebView */
}
</style>

6
微调一下,规则的签名应该是:*:not(input):not(textarea) - mtdb

1

我使用了以下代码,在Android和iOS设备以及模拟器上都可以正常工作:

 * {
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    }

   input, textarea {
    -webkit-user-select: auto !important;
    -khtml-user-select: auto !important;
    -moz-user-select: auto !important;
    -ms-user-select: auto !important;
    user-select: auto !important;
    }

0

对我来说,这是最好的:

-webkit-tap-highlight-color: rgba(0,0,0,0); 
tap-highlight-color: rgba(0,0,0,0);

我的情况发生在pixi.js中,使用了

plugins.interaction.autoPreventDefault = true;

0

最近更新到Android 4.4 KitKat版本,它解决了Edittext在taphold(长按)时显示的问题。显然,在Android 4.4上,使用jquery mobile和phonegap时,edittext不会在taphold上出现。我正在使用自定义aosp rom,因此尚未在官方版本上进行测试,但我猜测(并希望)它也适用于任何4.4版本。它似乎还解决了我遇到的其他与onclick冲突的问题,如下所述。

我发现的旧方法(可能存在问题),适用于旧版ICS roms(仅在4.0.4自定义rom上测试过)。
通过添加滚动条

示例:

<div id="scroll">content</div> 

<style>
#scroll {

height: 100%;
width: 100%;
}
</style>

它可以禁用EditText在手机gap和jQuery Mobile上的taphold(长按)弹出。目前还不确定它是否会导致任何冲突(因为似乎对onclick有一些影响,我正在解决),但对于常规文本应该效果良好。 --- 在Android 4.4中解决了问题,也不再需要滚动。 (请参见帖子顶部)

-1
* {
    -webkit-touch-callout: none;
    -webkit-user-select: none; 
}

[contenteditable="true"] , input, textarea {
    -webkit-user-select: auto !important;
    -khtml-user-select: auto !important;
    -moz-user-select: auto !important;
    -ms-user-select: auto !important;
    -o-user-select: auto !important;
    user-select: auto !important;  
}

1
请在您的答案中添加解释,这对未来的其他人可能会有帮助。[答案] - cosmoonot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接