将数据从View类传递到Android中的Activity类

时间:2013-08-31 04:24:47

标签: java android

在我的Android应用程序中,我将为鸟类素描着色。我需要用各种颜色为鸟的每个部分着色。我用这个位图的屏幕坐标识别那些部分。现在我需要的是,当用户触摸身体部位的区域时,然后打开另一个具有颜色列表的窗口。为此,我需要将数据从我的View类传递给新的Activity类。我怎样才能做到这一点?请帮忙!

我的观点类

class BirdColors extends View{

    private Paint paint;
    public Bitmap mBitmap,nBitmap;
    public Canvas canvas;
    private int x,y;
    private CreateColorList colorList;
    int val;

    public BirdColors(Context context) {
        super(context);
        // TODO Auto-generated constructor stub 
        paint=new Paint();
        paint.setAntiAlias(true);   
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);

        mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.birdsketchfinal2).copy(Config.ARGB_8888, true);


        mBitmap= Bitmap.createScaledBitmap(mBitmap, 230, 230, true);
        //mBitmap=nBitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);


        this.canvas=canvas;
        canvas.drawBitmap(mBitmap,0,0, null);
        //canvas.drawText("Shashika", 10, 50, paint);
        canvas.drawText("Screen Cordinates" +x+"x"+y, 10, 220, paint);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
         switch(event.getAction()){

        case MotionEvent.ACTION_DOWN:

            x=(int)event.getX();
            y=(int)event.getY();

            if((x>5 && x<23)&&(y>30 && y<47)){

                    val=1;
                    //colorList=new CreateColorList(val);
                    //In here I need to pass the val 

            }
            invalidate();
         }
        return true;
    }

3 个答案:

答案 0 :(得分:2)

您可以通过以这种方式创建界面来实现此目的

public class BirdColors extends View{

   //.........your code ............

// create local object of BirdColorListener

   private BirdColorsListener local;

// create seter/geter methods

   public void setBirdColorListener(BirdColorsListener birdColorListenr){
   this.local = birdColorListenr;
   }
   public BirdColorsListener setBirdColorListener{
     return this.local;
   }


    @Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
     switch(event.getAction()){

    case MotionEvent.ACTION_DOWN:

        x=(int)event.getX();
        y=(int)event.getY();

        if((x>5 && x<23)&&(y>30 && y<47)){

                val=1;
                //colorList=new CreateColorList(val);
                //In here I need to pass the val 

             if(getBirdColorListener()!=null){
               getBirdColorListener().onBirdTouch(val);
             }


        }
        invalidate();
     }
    return true;
}


   //Add this things

 public interface BirdColorsListener{

   void onBirdTouch(int val);
 }

}

在您的活动中;

public class BirdActivity extends Activity{

  BirdColors bc = new BirdColors();




  protected void onCreate(){

    bc.setBirdColorListener(new BirdColorsListener() {

            @Override
            public void onBirdTouch(int val) {
                // you will get "val"  from your view

            }
        });
  }

}

答案 1 :(得分:0)

构造自己的类时,可以传递外部Activity的对象的引用, 因此,当您在自己的视图中保存事件时,可以通过该引用调用该方法并将数据传递给它 如:

Class OuterActivity externd Activity{
 public void setVal(int color){
 }
...
...
 View yourView = new YourView(this);
}

所以在你看来:

在constructer中:

YourView(Activity inarg){
 this.mOuterActivity =inarg;
}

...//and hold the event

this.mOuterActivity.setVal(0x0099ff);

答案 2 :(得分:0)

我遇到了同样的问题,但是其他解决方案(可能是最好的解决方案)对我不起作用,所以我做了一个像这样的全局变量将数据从我的视图传递回我的活动: / p>

public static ArrayList<Coordinates> coordinates = new ArrayList<Coordinates>();

它位于一个名为GlobalStatic的独立类中,所以现在我可以在任何地方调用这个变量:

GlobalStatic.coordinates....

并将信息输入到我的视图中我只是在该视图中创建了一个函数并在我的活动中调用了该函数:

视野

public void setStatementId(int id) {
    statementId = id;
}
活动

BrushView S_map = new BrushView(this);
        S_map.setStatmentId(statementId);

这不是一个很好的解决方案,但它对我有用。