我有一个扩展 View 的 B 类,我从通过 setContentView 扩展 Activity 的 A 类调用它。在 B 类中,我可以使用鼠标运动侦听器和绘制函数绘制任何东西。现在我想在我的 B 类上添加按钮,以便我可以去 A 类并进行一些修改。我尝试了一些代码,但没有奏效。
public class MotionDraw extends View {
private Paint paint = new Paint();
private Path path = new Path();
//LinearLayout ll=new LinearLayout(getContext());
public MotionDraw(Context context, AttributeSet attrs,int x) {
super(context, attrs);
//this.setOrientation(LinearLayout.VERTICAL);
//Button b=new Button(getContext());
//b.setText("Back"); // better: getContext().getString(R.string.someString);
//this.addView(b);
paint.setAntiAlias(true);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
//paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.MITER);
if(x == 1)
paint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
some code ...
}
// Schedules a repaint.
invalidate();
return true;
}
}