gpt4 book ai didi

android - 将 Activity 对象传递给android中的非 Activity 类

转载 作者:太空狗 更新时间:2023-10-29 14:42:34 24 4
gpt4 key购买 nike

我有一个来自这样的 Activity 类的对象:

 CustomPaint customCanvasPaint=(CustomPaint) dialog.findViewById(R.id.customCanvasPaint);

如何像这样将这个对象传递给类:

public class CustomPaint extends CanvasView {
public CustomPaint(Context context) {
super(context);
initialize(context);
}

public CustomPaint(Context context, AttributeSet attrs) {
super(context, attrs);
initialize(context);
}


public void initialize(Context context){
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.custom_paint,null);
CanvasView canvasView = (CanvasView) view.findViewById(R.id.customCanvasView);
canvasView.setPaintStrokeColor(Color.RED);
canvasView.setPaintStrokeWidth(12f);
}
}

我想在初始化方法中自定义对象,但绘制颜色和绘制宽度不变。我该怎么做?谢谢。

最佳答案

public class CustomPaint extends CanvasView {
CanvasView mCanvasView;
public CustomPaint(Context context,CanvasView canvasView) {
super(context);
this.mCanvasView = canvasView;
initialize(context);
}

public CustomPaint(Context context, AttributeSet attrs,CanvasView canvasView) {
super(context, attrs);
this.mCanvasView = canvasView;
initialize(context);
}

public void initialize(Context context){

canvasView.setPaintStrokeColor(Color.RED);
canvasView.setPaintStrokeWidth(12f);
}
}

和表单 Activity

 CanvasView canvasPaint=(CanvasView) dialog.findViewById(R.id.canvasPaint);
CustomPaint mCustomPaint = new CustomPaint(this,canvasPaint);

关于android - 将 Activity 对象传递给android中的非 Activity 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45408133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com