2012-10-01 47 views
-1

任何一个可以告诉我如何箱视图类中的onclicklistenr ..OnClickListener上视图类

我创建的应用程序一样,采取图像从相机..在画布上绘制我使用框架布局创造了一些自定义按钮我的看法类中。但我不能创建视图类内部的onclicklistener ....

这里是我的子活动,在画布上绘制

public class MesureSizeActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.button); 
    } 
    } 

和我的视图类

public class MyView extends View { 



private Bitmap mBitmap; 
private Canvas mCanvas; 
private Path mPath; 
private Paint mBitmapPaint; 
private Paint mPaint; 
boolean calibrate=true; 
int calibrate_x; 
int calibrate_y; 
int calibrate_radius; 

//Event listener controller 

boolean listener_calibrate= true; 

public MyView(Context c) { 
    super(c); 
    mBitmap = MainActivity.getBitmap(); 
    mPath = new Path(); 
    mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(0xFFFFFF11); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    // mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(2); 
} 

public MyView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mBitmap = MainActivity.getBitmap(); 
    mPath = new Path(); 
    mBitmapPaint = new Paint(Paint.DITHER_FLAG); 

    mPaint = new Paint(); 
    mPaint.setAntiAlias(true); 
    mPaint.setDither(true); 
    mPaint.setColor(0xFFFFFF11); 
    mPaint.setStyle(Paint.Style.STROKE); 
    mPaint.setStrokeJoin(Paint.Join.ROUND); 
    // mPaint.setStrokeCap(Paint.Cap.ROUND); 
    mPaint.setStrokeWidth(2); 
} 

public MyView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 


@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 
    mBitmap = Bitmap.createScaledBitmap(mBitmap, w, h, true); 
    mCanvas = new Canvas(mBitmap); 
    calibrate_x = w /2; 
    calibrate_y = h/2; 
    calibrate_radius = 50; 

} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawColor(0xFFAAAAAA); 

    canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

     canvas.drawCircle(calibrate_x,calibrate_y,calibrate_radius,mPaint); 
    canvas.drawPath(mPath, mPaint); 

} 

private float mX, mY; 
private static final float TOUCH_TOLERANCE = 4; 

private void touch_start(float x, float y) { 
    mPath.reset(); 
    mPath.moveTo(x, y); 
    mX = x; 
    mY = y; 
} 

private void touch_move(float x, float y) { 
    float dx = Math.abs(x - mX); 
    float dy = Math.abs(y - mY); 
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
     // mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
     mX = x; 
     mY = y; 
    } 
} 

private void touch_up() { 
    mPath.lineTo(mX, mY); 
    // commit the path to our offscreen 
    mCanvas.drawPath(mPath, mPaint); 
    // kill this so we don't double draw 
    mPath.reset(); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    float x = event.getX(); 
    float y = event.getY(); 

    switch (event.getAction()) { 
    case MotionEvent.ACTION_DOWN: 
     touch_start(x, y); 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_MOVE: 
     touch_move(x, y); 
     invalidate(); 
     break; 
    case MotionEvent.ACTION_UP: 
     touch_up(); 

     invalidate(); 
     break; 
    } 
    return true; 
    } 

public void onClick(View v) { 

    if(v.getId() == R.id.up) 
    { 
    //Do some thing 


    } 

if(v.getId() == R.id.down) 
{ 
    //Do some thing 


    } 

} 

} 

而且我的XML看起来像

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
     android:visibility="visible" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 

<com.example.sizemesurment_1.MyView 
    android:id="@+id/DrawViewId" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    </com.example.sizemesurment_1.MyView > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:gravity="bottom"> 



    <ImageButton 
     android:id="@+id/up" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginLeft="80dp" 
     android:onClick="ButtonOnClick"   
     android:src="@drawable/up" /> 

    <ImageButton 
     android:id="@+id/down" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginLeft="120dp" 
     android:onClick="ButtonOnClick"  
     android:src="@drawable/down" /> 


    </RelativeLayout> 
</FrameLayout> 

虽然我按我的错误,如

按钮

找不到在活动课com.example.sizemesurment_1的方法ButtonOnClick(查看) .MesureSizeActivity对视图类android.widget.Button的onClick处理程序ID为“上”

难道我做错了什么。我是一个初学者.... 请帮助.......

在此先感谢.....

回答

2

添加ButtonOnClick方法在活动课...

public class MesureSizeActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.button); 
    } 

    public void ButtonOnClick(View view){ 
      //here you can handle click event 
    } 
    } 
+0

海萨米尔Mangroliya感谢您的帮助....它的工作... – Haris

相关问题