0

嗨,我想创建简单的应用程序。我使用一个名为 Drawcanvas 的自定义类,用于绘制运行时画布。所以我使用 ontouchListener 和 OnClicklistener onthis。但这些事件无法正常工作。我的代码如下。 这是我使用自定义类名的类,如 DrawCanvas

public class CanvasExample extends Activity 
{
    /** Called when the activity is first created. */

    RelativeLayout relMainOperationLayout;
    RelativeLayout relTabHeader;
    RelativeLayout relMidalLayout;
    RelativeLayout relBelowLayout;
    Context myContext;
    DrawCanvas drawCanvas;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        myContext=CanvasExample.this;

        LayoutInflater layoutInflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);   

        int layoutId = myContext.getResources().getIdentifier("main","layout",getPackageName());

        relMainOperationLayout = (RelativeLayout) layoutInflater.inflate(layoutId,null);

        relTabHeader=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relHeadLayout);

        relMidalLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relmidalLayout);

        relBelowLayout=(RelativeLayout) relMainOperationLayout.findViewById(R.id.relBelowLayout);

        drawCanvas=new DrawCanvas(CanvasExample.this,myContext);
        drawCanvas.setBackgroundColor(Color.YELLOW);
        RelativeLayout.LayoutParams drawParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,400);
        drawParams.addRule(RelativeLayout.BELOW, relTabHeader.getId());
        //relMidalLayout.addView(drawCanvas,drawParams);
        relMainOperationLayout.addView(drawCanvas,drawParams);
        setContentView(relMainOperationLayout);
    }

这是我扩展 View 的 CustomClass 代码。名称 DrawCanvas

public class DrawCanvas extends View  implements  View.OnTouchListener,View.OnClickListener
{

    Context drawContext;
    Activity drawActivity;
    public DrawCanvas(Activity activity,Context context)
    {
        super(activity);
        this.drawActivity=activity;
        this.drawContext=context;
    }
    @Override
    public void onClick(View v) 
    {
        System.err.println("Click Here");
        Toast.makeText(drawContext, "Click ", 1000).show();

    }
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        System.err.println("Touch Here");
        return true;
    }

}

我是帆布新手。

4

1 回答 1

4

您必须使用setOnTouchListenerand setOnClickListener(尽管我认为点击事件不会帮助您)为您的View.

public DrawCanvas(Activity activity,Context context) {
    super(activity);
    this.drawActivity=activity;
    this.drawContext=context;
    setOnTouchListener(this);
    setOnClickListener(this);
}
于 2012-08-04T06:00:07.517 回答