我目前正在尝试获取一个自定义 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
}
};
}
这可以毫无问题地检测到 longPress 并调用 onLongPress 方法,但是在显示 ContextMenu 时我不知所措。我尝试在我的活动中以通常的方式进行操作:
@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?