我希望禁用原生上下文菜单,该菜单会在选择某些文本时显示,其中包括“全选”,“复制”,“分享”和“搜索”按钮。然而,我并不想禁用选择本身。理想情况下,我希望扩展菜单,但是实际上,我只需要禁用它就足够了。从我找到的文档来看,对于文本字段等,这通常相对简单,但我无法弄清如何在XWalkView/CordovaWebView中实现此功能。也许我正在完全错误的角落寻找答案。
我有一个解决方案。
对于WebView
,有一个解决方案,但不适用于XWalkView
:
我的gradle包括compile 'org.xwalk:xwalk_core_library:14.43.343.17'
我的解决方案,在onAttachedToWindow
方法中的主要思路:
public class XWalkWebView extends XWalkView {
public XWalkWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private ActionMode.Callback mOriginalCallback;
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
try {
View innerChild = ((ViewGroup) getChildAt(0)).getChildAt(0);
Field contentViewField = innerChild.getClass().getDeclaredField("mContentView");
contentViewField.setAccessible(true);
XWalkContentView xWalkContentView = (XWalkContentView) contentViewField.get(innerChild);
Field contentViewCoreField = xWalkContentView.getClass().getSuperclass().getDeclaredField("mContentViewCore");
contentViewCoreField.setAccessible(true);
ContentViewCore viewCore = (ContentViewCore) contentViewCoreField.get(xWalkContentView);
viewCore.setContainerView(this);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
mOriginalCallback = callback;
ActionMode.Callback c = new // your callback...
return super.startActionMode(c);
}
}
我尝试了Warabei的解决方案,但它在15.44.384.13上无法工作。我改进了查找ContentViewCore跨版本的方法:
public class XWalkWebView extends XWalkView {
...
private Field getFields(Class clazz){
for(Field field:clazz.getDeclaredFields()){
if(ContentViewCore.class == field.getType()){
return field;
}
}
clazz = clazz.getSuperclass();
if(clazz!=null && clazz!=Object.class){
Field field = getFields(clazz);
if(field!=null)return field;
}
return null;
}
private void inject(View view){
Field field = getFields(view.getClass());
if(field!=null){
field.setAccessible(true);
try {
ContentViewCore viewCore = (ContentViewCore) field.get(view);
viewCore.setContainerView(this);
return;
}catch(Exception e){
}
}
if(view instanceof ViewGroup){
ViewGroup viewGroup = (ViewGroup)view;
int count = viewGroup.getChildCount();
for(int i = 0;i<count;i++){
inject(viewGroup.getChildAt(i));
}
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
inject(this);
}
...
@Override
public ActionMode startActionMode(ActionMode.Callback callback) {
return new ActionMode() {
@Override
public void setTitle(CharSequence charSequence) {
}
@Override
public void setTitle(int i) {
}
@Override
public void setSubtitle(CharSequence charSequence) {
}
@Override
public void setSubtitle(int i) {
}
@Override
public void setCustomView(View view) {
}
@Override
public void invalidate() {
}
@Override
public void finish() {
}
@Override
public Menu getMenu() {
return null;
}
@Override
public CharSequence getTitle() {
return null;
}
@Override
public CharSequence getSubtitle() {
return null;
}
@Override
public View getCustomView() {
return null;
}
@Override
public MenuInflater getMenuInflater() {
return null;
}
};
}
这是一篇旧帖子,但我还没有找到其他解决方案。
一个简单的解决方法来禁用Crosswalk视图中的上下文选项..
这个CSS应该可以在Android和IOS上阻止上下文菜单,就像cordova模板中所示
* {
-webkit-tap-highlight-color: rgba(0,0,0,0); /* make transparent link selection, adjust last value opacity 0 to 1.0 */
}
body {
-webkit-touch-callout: none; /* prevent callout to copy image, etc when tap to hold */
-webkit-text-size-adjust: none; /* prevent webkit from resizing text to fit */
-webkit-user-select: none; /* prevent copy paste, to allow, change 'none' to 'text' */
}