Android 在 PhoneGap 中禁用文本选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11820708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:57:27  来源:igfitidea点击:

Disabling text selection in PhoneGap

androidcordovatextselection

提问by stmn

Is it possible to disable text selection to make a PhoneGap app more similar to normal native app?

是否可以禁用文本选择以使 PhoneGap 应用程序更类似于普通本机应用程序?

Something like this:

像这样的东西:

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

or:

或者:

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

Or many other things don't work.

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

回答by ThinkingStiff

Putting it on html, not *, works for me:

穿上它html,而不是*,对我有用:

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

回答by habermanm

I looked all over for help on this. This finally worked for me.

我四处寻找这方面的帮助。这最终对我有用。

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;
            }
        });
    }
}

The setOnClickListener is what does the magic. Make sure you put this AFTER your call to super.loadUrl.

setOnClickListener 有什么神奇之处。确保在调用 super.loadUrl 之后放置它。

Of course, this will disable text selection for your entire app, but I'm OK with that, and I don't have any other way around it for now.

当然,这将禁用整个应用程序的文本选择,但我对此没有意见,而且我现在没有任何其他方法可以解决它。

I'm not sure of the complete implications of this yet, but I do make use of the JqueryMobile event "taphold", and it still works fine. I believe this works by handling the long click on the appView (which hosts your HTML app) and preventing it from bubbling up.

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

回答by nargil

this will work too.

这也会起作用。

<body oncontextmenu="return false" ondragstart="return false" 
onselectstart="return false">

回答by mattdryden

As well as what ThinkingStiff mentioned I also use the following to remove any highlighting/copy & paste

除了 ThinkingStiff 提到的内容,我还使用以下内容删除任何突出显示/复制和粘贴

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

回答by atulkhatri

Add this and enjoy. Works on iOS as well.

添加这个并享受。也适用于 iOS。

<style type="text/css">
*:not(input,textarea) {
    -webkit-touch-callout: none;
    -webkit-user-select: none; /* Disable selection/Copy of UIWebView */
}
</style>

回答by Vikasdeep Singh

I used below code and its working fine on Android and iOS devices as well as on emulator/simulator:

我使用了下面的代码,它在 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;
    }

回答by csomakk

For me this was the best:

对我来说这是最好的:

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

My case happened with pixi.js, with

我的情况发生在 pixi.js 上,

plugins.interaction.autoPreventDefault = true;

回答by watson7d

Recently updated to Android 4.4 kitkat and it resolved the problem of Edittext showing up on taphold (long press). Apparently edittext doesn't showup on taphold with jquery mobile and phonegap on android 4.4. I am using a custom aosp rom so haven't tested on an official release but am guessing (and hoping) it should work as well for any 4.4 release. It also seems to resolved other conflicts I was having with onclick which I posted below.

最近更新到 Android 4.4 kitkat 并解决了 Edittext 显示在 taphold(长按)上的问题。显然,在 Android 4.4 上使用 jquery mobile 和 phonegap 时,edittext 不会显示在 taphold 上。我使用的是定制的 aosp rom,所以还没有在官方版本上进行测试,但我猜测(并希望)它应该适用于任何 4.4 版本。它似乎也解决了我在下面发布的 onclick 遇到的其他冲突。

Older method I found that works (with possible issues) for older ICS roms (only tested on 4.0.4 custom rom).
By adding a scrollbar

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

example:

例子:

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

<style>
#scroll {

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

It disables the EditText from popping on taphold (long press) for phonegap and jquery mobile as well. Not sure yet if this will cause any conflicts (as seems to have some effects with onclick which am looking at sorting out) but with regular text should work well.--- Issues sorted out with Android 4.4 and no need for scroll either anymore. (see top of post)

它也禁用了 EditText 在用于 phonegap 和 jquery mobile 的 taphold(长按)上弹出。尚不确定这是否会导致任何冲突(因为似乎对正在整理的 onclick 有一些影响),但使用常规文本应该可以正常工作。--- 问题已通过 Android 4.4 解决,不再需要滚动。(见帖子顶部)

回答by Abdenacer Rachedi

* {
    -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;  
}