Android:在自定义 WebView 中从 onLongPress 打开 ContextMenu

发布于 2024-10-15 15:02:56 字数 1560 浏览 5 评论 0原文

我目前正在尝试获取一个自定义 WebView,当按下较长时间时该 WebView 会显示 ContextMenu。由于默认的 WebView 类仅在长按链接时显示 ContextMenu,因此我编写了自己的类来覆盖此行为:

public class MyWebView extends WebView {
    Context context;
    GestureDetector gd;

    public MyWebView(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.context = context;
        gd = new GestureDetector(context, sogl);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }

    GestureDetector.SimpleOnGestureListener sogl =
                new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }

        public void onLongPress(MotionEvent event) {
            // The ContextMenu should probably be called here
        }
    };
}

这可以正常工作,检测到长按并调用 onLongPress 方法,但是当涉及到显示上下文菜单。我尝试在 Activity 中以通常的方式执行此操作:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv);
    registerForContextMenu(mwv);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}

但是,当我在模拟器中长按 MyWebView 时,没有任何反应。我必须从 onLongPress() 调用什么才能显示 ContextMenu?

I'm currently trying to get a custom WebView that displays a ContextMenu when it is pressed for a longer time. As the default WebView class only displays a ContextMenu when a link is longPressed, I wrote my own class to override this behaviour:

public class MyWebView extends WebView {
    Context context;
    GestureDetector gd;

    public MyWebView(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.context = context;
        gd = new GestureDetector(context, sogl);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }

    GestureDetector.SimpleOnGestureListener sogl =
                new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }

        public void onLongPress(MotionEvent event) {
            // The ContextMenu should probably be called here
        }
    };
}

This works without problems the longPress is detected and the onLongPress method is called, however I am at a loss when it comes to displaying the ContextMenu. I tried doing it the usual way in my Activity:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv);
    registerForContextMenu(mwv);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}

However, when I longPress the MyWebView in the emulator, nothing happens. What do I have to call from onLongPress() to display the ContextMenu?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三岁铭 2024-10-22 15:02:57

我注意到,长按模拟器中的任何内容都需要大量的按压,例如 5-7 秒,而不是现实生活中常规的 1-2 秒。确保按下至少 10 秒钟,否则看起来好像什么也没有发生。

I noticed that long pressing anything in the emulator requires a LOT of pressing, like 5-7 seconds as opposed to a regular 1-2 in real life. Make sure you press for at least 10 seconds, otherwise it would seem like nothing happens.

ゃ懵逼小萝莉 2024-10-22 15:02:56

根据 nggr44 的建议,我现在可以使用它了。我让我的活动实现了 OnLongClickListener 类,并提供了一个打开上下文菜单的 onLongClick() 方法。

修改后的代码:

自定义WebView:

public class MyWebView extends WebView {
    MyActivity theListener;
    Context context;
    GestureDetector gd;

    public MyWebView(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.context = context;
        gd = new GestureDetector(context, sogl);
    }

    // This is new
    public void setListener(MyActivity l) {
        theListener = l;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }

    GestureDetector.SimpleOnGestureListener sogl =
                new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }

        public void onLongPress(MotionEvent event) {
            theListener.onLongClick(MyWebView.this);
        }
    };
}

我的活动:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv);
    registerForContextMenu(mwv);
}

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

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}

I got it working now, building on gngr44's suggestion. I made my activity implement the OnLongClickListener class and provided a onLongClick() method that opens the context menu.

The revised code:

The custom WebView:

public class MyWebView extends WebView {
    MyActivity theListener;
    Context context;
    GestureDetector gd;

    public MyWebView(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.context = context;
        gd = new GestureDetector(context, sogl);
    }

    // This is new
    public void setListener(MyActivity l) {
        theListener = l;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }

    GestureDetector.SimpleOnGestureListener sogl =
                new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }

        public void onLongPress(MotionEvent event) {
            theListener.onLongClick(MyWebView.this);
        }
    };
}

My Activity:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv);
    registerForContextMenu(mwv);
}

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

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}
旧伤慢歌 2024-10-22 15:02:56

我建议不要从视图中访问活动,而是在视图中使用接口并从活动中实现该接口。

public class MyWebView extends WebView {
    private OnLongPressListener mListener;

    public MyWebView(Context context, AttributeSet attributes) {
        mListener = (OnLongPressListener) context;
    }

    public void onLongPress(MotionEvent event) {
        mListener.onLongPress(your variables);
    }

    public interface OnLongPressListener {
        public void onLongPress(your variables);
    }
}

public class YourActivity extends Activity implements OnLongPressListener {

    @Override
    public void onLongPress(your variables) {
        // handle the longPress in your activity here:
    }
}

Instead of accessing the activity from your view I would recommend using an Interface in your View and implementing that interface from your Activity.

public class MyWebView extends WebView {
    private OnLongPressListener mListener;

    public MyWebView(Context context, AttributeSet attributes) {
        mListener = (OnLongPressListener) context;
    }

    public void onLongPress(MotionEvent event) {
        mListener.onLongPress(your variables);
    }

    public interface OnLongPressListener {
        public void onLongPress(your variables);
    }
}

public class YourActivity extends Activity implements OnLongPressListener {

    @Override
    public void onLongPress(your variables) {
        // handle the longPress in your activity here:
    }
}
榆西 2024-10-22 15:02:56

在onLongPress中调用Activity.openContextMenu(View v)。这意味着 MyWebView 保留对 Activity 的引用。

Call Activity.openContextMenu(View v) in onLongPress. This would mean having the MyWebView keep a reference to the Activity though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文