谁能告诉我如何在视图类中创建一个 onclicklistenr ..
我创建了一个应用程序,例如通过从相机获取图像在画布上绘图。我在视图类中使用框架布局创建了一些自定义按钮。但我无法在视图类中创建 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>
当我按下按钮时,出现如下错误
在 id 为“up”的视图类 android.widget.Button 上的 onClick 处理程序的活动类 com.example.sizemesurment_1.MesureSizeActivity 中找不到方法 ButtonOnClick(View)
我做错什么了吗..我是初学者....请帮忙....
提前致谢.....