Android:在自定义 WebView 中从 onLongPress 打开 ContextMenu
我目前正在尝试获取一个自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我注意到,长按模拟器中的任何内容都需要大量的按压,例如 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.
根据 nggr44 的建议,我现在可以使用它了。我让我的活动实现了 OnLongClickListener 类,并提供了一个打开上下文菜单的 onLongClick() 方法。
修改后的代码:
自定义WebView:
我的活动:
I got it working now, building on gngr44's suggestion. I made my activity implement the
OnLongClickListener
class and provided aonLongClick()
method that opens the context menu.The revised code:
The custom WebView:
My Activity:
我建议不要从视图中访问活动,而是在视图中使用接口并从活动中实现该接口。
Instead of accessing the activity from your view I would recommend using an Interface in your View and implementing that interface from your Activity.
在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.