0

こんにちは、単純なアプリを作成したいです。ランタイム キャンバスを描画することを目的とした Drawcanvas のような名前のカスタム クラスを 1 つ使用します。しかし、それらのイベントは機能しません。私のコードは以下です。 これは、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 に答える