简体   繁体   English

禁用人行横道(cordova)中的上下文选择菜单

[英]Disable contextual selection menu in crosswalk (cordova)

I wish to disable the native contextual menu that is shown when you select some text, the one with the select all , copy , share and search buttons. 我希望禁用选择某些文本时显示的本机上下文菜单, 选择全部复制共享搜索按钮。 I do not however want to disable selections themselves. 但是,我不想自己禁用选择。 Ideally I would wish to extend the menu actually, but honestly, I am more than perfectly fine with just disabling it. 理想情况下,我希望实际扩展菜单,但说实话,我只是禁用它完全没问题。 With textfields and the like it tends to be relatively simple from the documentation I found, but I just can't figure out a way to make this work with XWalkView / CordovaWebView . 对于文本字段等,从我发现的文档中它往往相对简单,但我无法找到一种方法来使用XWalkView / CordovaWebView Might be that I am just searching in entirely the wrong corner though. 可能是因为我只是在完全错误的角落里寻找。

I have a workaround. 我有一个解决方法。

For WebView there is a solution, but it doesn't work for XWalkView : 对于WebView有一个解决方案,但它不适用于XWalkView

WebView selection menu workaround WebView选择菜单解决方法

My gradle includes compile 'org.xwalk:xwalk_core_library:14.43.343.17' 我的gradle包括compile 'org.xwalk:xwalk_core_library:14.43.343.17'

My solution, the main idea in the onAttachedToWindow method: 我的解决方案, 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);
  }

}

I try Warabei's solution but it not work on 15.44.384.13. 我尝试了Warabei的解决方案,但它不适用于15.44.384.13。 I improve to find ContentViewCore cross versions: 我改进了以查找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);
    }
    ...

To disable contextual selection menu: 要禁用上下文选择菜单:

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

It is an old post but I haven't been able to find another solution. 这是一个老帖子,但我找不到另一个解决方案。

A simple workaround to disable context options in crosswalk view.. 在人行横道视图中禁用上下文选项的简单解决方法..

  1. Go to your crosswalk project into res/menu/select_action_menu.xml 将您的人行横道项目转到res / menu / select_action_menu.xml
  2. Delete or comment on the item you don't want to show 删除或评论您不想显示的项目
  3. Save, build and run 保存,构建和运行

This CSS should prevent context menus in both Android and IOS, as given in the cordova template 这个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' */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM