如何使用在另一个类中声明的数组

时间:2011-12-06 02:01:43

标签: java android

我是Java新手。这是我的代码。我在onCreate()创建了一个数组,如何在onDraw()中使用它?谢谢!

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int Circle_list[] = { Color.BLACK, 20, 20, 50 };
}

private class MyViewCircle extends View {

    public MyViewCircle(Context context) {
        super(context);

        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3], paint);
    }
}

3 个答案:

答案 0 :(得分:1)

目前,您的数组是仅在'onCreate'方法中知道的局部变量。您必须通过将其定义为实例变量来更改其范围,以便onDraw可以访问它。

    private int[] Circle_list = null ;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);         
        Circle_list = new int[] { Color.BLACK, 20, 20, 50 };     
    }  
    private class MyViewCircle extends View {          
        public MyViewCircle(Context context) {
            super(context);              
        // TODO Auto-generated constructor stub         
        }
        @Override
        protected void onDraw(Canvas canvas) {
           // TODO Auto-generated method stub
           super.onDraw(canvas);
           Paint paint = new Paint();
           paint.setAntiAlias(true);
           paint.setColor(Color.WHITE);
           canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3],
                paint);         
        }     
    }

答案 1 :(得分:1)

在MyCircleView类中创建一个传入圆形数组的方法。

public class MyViewCircle extends View {
    int[] circle_list;

    public MyViewCircle(Context context) {
        super(context);

        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3],
                paint);
    }

    public void setCircleList(int[] circles) {
        circle_list = circles;
    }
}

然后在您的活动的onCreate方法中,执行以下操作:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    int Circle_list[] = { Color.BLACK, 20, 20, 50 };
    MyViewCircle view = (MyViewCircle) findViewById(R.id.circle_view);
    view.setCircleList(Circle_list);
}

(对于R.id.circle_view,请替换适用于xml中定义的圆圈视图的任何ID。)

答案 2 :(得分:0)

如果使Circle_list成为外部类的成员变量,则可以从内部类访问该变量。

您的代码当前在方法中声明了Circle_list,因此在该方法之外无法访问它。下面的代码应该告诉你如何做到这一点。请注意,您的数组语法不太正确,它应该是“type [] var”形式,而不是“type var []”

class MyClass extends Something {

    //Declare variables here
    private int[] Circle_list; //Note the int[], not int var[] as in C/C++

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int[] Clist = { Color.BLACK, 20, 20, 50 };
        Circle_list = Clist;
    }

    private class MyViewCircle extends View {

        public MyViewCircle(Context context) {
            super(context);

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.WHITE);
            canvas.drawCircle(Circle_list[1], Circle_list[2], Circle_list[3],
                paint);
        }
    }