如何上载自定义视图以使用上下文菜单

时间:2018-07-13 17:30:39

标签: java android

当用户在我的自定义视图中保存某些内容时,我试图使用上下文菜单弹出一些选项。

现在我的自定义视图扩展了View,所以我不明白为什么我无法让registerForContextMenu()对其进行处理。

在我的活动中:

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

    photoSorter = (PhotoSortrView) findViewById(R.id.photosortr);
    //View sorterView = (View)photoSorter;
    registerForContextMenu(photeSorter); //tried using sorterView as well
}

我像正常情况一样调用上下文菜单(它也适用于我为其分配的其他任何内容)

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    Log.d("PRESS","OnCreatContextMenu");
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}

作为参考,这是我的自定义View类的外观(仅显示覆盖的方法):

public class PhotoSortrView extends View implements

    MultiTouchObjectCanvas<MultiTouchEntity> {


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    canvas.drawPath(drawPath, drawPaint);
    int n = imageIDs.size();
    for (int i = 0; i < n; i++)
        imageIDs.get(i).draw(canvas);
}

/** Pass touch events to the MT controller */
@Override
public boolean onTouchEvent(MotionEvent event) {
    float touchX = event.getX();
    float touchY = event.getY();
    // respond to down, move and up events
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //handler.postDelayed(mLongPressed, 
ViewConfiguration.getLongPressTimeout());
        drawPath.moveTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_MOVE:
        drawPath.lineTo(touchX, touchY);
        break;
    case MotionEvent.ACTION_UP:
        drawPath.lineTo(touchX, touchY);
        drawCanvas.drawPath(drawPath, drawPaint);
        drawPath.reset();
        break;
    default:
        return false;
    }
    return multiTouchController.onTouchEvent(event);
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    if (w > 0 && h > 0) {

        super.onSizeChanged(w, h, oldw, oldh);
        canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        drawCanvas = new Canvas(canvasBitmap);
    }
}

}

任何帮助将不胜感激

0 个答案:

没有答案