[英]Android How to Call Method From Another Class
现在我正在研究将在onClick
划线的应用程序。 我在LineView.java
类中绘制线条并在MainActivity.java
执行onClick
方法。 为了解决这个问题,我检查了类似的问题。 第一个解决方案:
LineView.onDraw();
它给了我这个错误:
Multiple markers at this line
- The method onDraw(Canvas) in the type LineView is not applicable for the
arguments ()
- Suspicious method call; should probably call "draw" rather than "onDraw"
我还尝试在MainActivity中编写:
LineView lineView = new LineView(null);
lineView.onDraw();
但它也给出了一个错误:
Multiple markers at this line
- The method onDraw(Canvas) in the type LineView is not applicable for the
arguments ()
- Suspicious method call; should probably call "draw" rather than "onDraw"
这是我的LineView.java:
public class LineView extends View {
Paint paint = new Paint();
Point A;
Point B;
boolean draw = false;
public void onCreate(Bundle savedInstanceState) {
}
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LineView(Context context, AttributeSet attrs, int defstyle) {
super(context, attrs, defstyle );
}
public LineView(Context context) {
super(context);
paint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
draw = MainActivity.draw;
if(draw){
//A = (getIntent().getParcelableExtra("PointA"));
A = MainActivity.A;
B = MainActivity.B;
canvas.drawLine(A.x, A.y, B.x, B.y, paint);
}
}
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
}
}
我的MainActivity.java
onClick
:
@Override
public void onClick(View v) {
draw = true;
LineView.onDraw();
}
});
提前致谢!
您不应该直接在任何View上调用onDraw()。 如果视图在视图层次结构中可见,则视图将自行绘制。 如果你需要视图来绘制自己因为某些东西已经改变了(比如你的A和B变量),那么你应该这样做:
LineView.invalidate();
invalidate()告诉UI系统视图已经改变,onDraw()应该在不久的将来某个时候调用(可能是UI线程的下一次迭代)。
我认为你的'draw'变量可能是不必要的。 如果您想有时隐藏视图,请改用setVisibility()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.