我有一项活动。 在活动中,我在onCreate中有这个:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = (GlobalSettings) getApplication();
mappingServiceContent = settings.getMappingServiceContent();
mapView = settings.getMapView();
if (mapView == null)
{
mapView = new RMapView(RCoordinateSystemFactory.createFromCrsCode("EPSG:2170"));
settings.setMapView(mapView);
settings.isFirstTime = true;
}
else
{
settings.isFirstTime = false;
}
RelativeLayout relativeLayout = new RelativeLayout(this);
myMapView = new MyMapView(this, this.getWindowManager(), settings);
zoomControls = new ZoomControls(this);
registerForContextMenu(myMapView );
RelativeLayout.LayoutParams mapViewLayoutParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT );
relativeLayout.addView(myMapView, mapViewLayoutParams);
RelativeLayout.LayoutParams zoomControlsLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
zoomControlsLayoutParams.addRule (RelativeLayout.ALIGN_PARENT_BOTTOM);
zoomControlsLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
relativeLayout.addView(zoomControls,zoomControlsLayoutParams);
zoomControls.setOnZoomInClickListener(new View.OnClickListener() {
public void onClick(final View v) {
myMapView.ZoomIn();
}
});
zoomControls.setOnZoomOutClickListener(new View.OnClickListener() {
public void onClick(final View v) {
myMapView.ZoomOut();
}
});
myMapView.setClickable(true);
myMapView.setEnabled(true);
setContentView(relativeLayout);
}
MyMapView是一个像这样创建的视图:
public class MyMapView extends View
在我的活动中,我添加了:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Action 1");
menu.add(0, v.getId(), 0, "Action 2");
}
但如果我将手指放在屏幕上,上下文菜单永远不会被触发。 我一定错过了什么......有些人可以帮帮我吗?
问候。
答案 0 :(得分:1)
也许你只是缺少sg。 AFAIK你的代码应该工作。如果您无法获得解决方案,请尝试使用AlertDialog,它是相同的(或者看起来和行为相同,idk)。
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
代码来自here
答案 1 :(得分:1)
基于您在MyMapView中使用onTouch的事实,您可以执行类似这样的操作。只需在事件中返回false,就不会被消耗掉。
public class MyButtonMenuAndTouch extends Button {
private static final String TAG = MyButtonMenuAndTouch.class.getName();
private GestureDetector mGestureDetector;
public MyButtonMenuAndTouch(Context context) {
super(context);
this.setText("My button with context menu and touch");
mGestureDetector = new GestureDetector(new MyGestureDetector());
this.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)) {
Log.d(TAG, "onTouchEvent");
}
return false;
}
});
this.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.d(TAG, "onLongClickEvent");
return false;
}
});
}
private class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.d(TAG, "onFling() - MyGestureDetector");
return true;
}
@Override
public boolean onDown(MotionEvent e) {
Log.d(TAG, "onDown() - MyGestureDetector");
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d(TAG, "onSingleTapUp() - MyGestureDetector");
return true;
}
}
}
答案 2 :(得分:0)
您需要在给定的资源菜单中使用menuInflater
来充气,请参阅此示例,它将非常有用Menu