简体   繁体   中英

Pass data from class to activity

I have an activity and an extended View class,that both use the same xml layout. In the extended view I do some drawings on screen with touch, and want to pass the coordinate x of the touch to the Activity, whenever the user taps the screen.

So far I have used a getter method and call it from inside the activity but it ,only, passes the initializing value of X of the constructor and not the actual X coordinate the user has touched.

I am pretty sure I am missing something. Can you give me some directions?

EDIT : As suggested, instead of the getter practice, I tried using a public interface, still with X not changing. Here is my code:

My custom class:

public class ImageView1 extends View  {

 Context context;

 ArrayList<ViewWasTouchedListener> listeners = new 
                                   ArrayList<ViewWasTouchedListener>();
 ImageView1 img = (ImageView1) findViewById (R.id.imageView1);

 public float x=0;
 public float y=0;

 public ImageView1(Context context) {
        super(context);
        this.context=context;
        setFocusable(true);
    }


public ImageView1(Context context, AttributeSet attrs) {
    super(context, attrs); 
    this.context=context;
    setFocusable(true);

} 

@Override
public boolean onTouchEvent(MotionEvent event){
        switch (event.getAction()){
        case (MotionEvent.ACTION_DOWN): {

                 x = event.getX();
                 y = event.getY();
     for (ViewWasTouchedListener listener:listeners){
         Log.d("Touchpoint", String.valueOf(x) +"," + String.valueOf(y));
         listener.onViewTouched(x);
        }
    }

    return true;

   }  

@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);

    ....
}

public void setWasTouchedListener(ViewWasTouchedListener listener){
    listeners.add(listener);
}   
}

My Main Activity:

 public class TargetPractise extends Activity implements ViewWasTouchedListener  {
 @Override

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.target_form);

    ImageView1 value = new ImageView1(TargetPractise.this);
    value.setWasTouchedListener(TargetPractise.this);   

 }

  public void onViewTouched(float x){
     Log.d("x",String.valueOf(x));

  }   
  }

My interface:

  public interface ViewWasTouchedListener {
void onViewTouched(float x);
 }

My xml layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >

 <android.archer.test.ImageView1
  android:id="@+id/imageView1"
  android:layout_width="300dip"
  android:layout_height="300dip"
  android:layout_gravity="center_horizontal"
  android:background="@drawable/target" >
</android.archer.test.ImageView1>

Define an interface and use a callback to let the activity know that x has changed. I like this approach because it works for any class interested in knowing when x has changed, not just your activity. Imagine you add a new type of view class but it also needs to know when your custom view has been touched (I do this with a graph and horizontal and vertical scales which can be dragged - the scales tell anyone who's interested that they have changed).

public Interface ViewWasTouchedListener {
    void onViewTouched(float x);
}

In your custom view

ArrayList<ViewWasTouchedListener> listeners = new ArrayList<ViewWasTouchedListener>();

...

public void setWasTouchedListener(ViewWasTouchedListener listener){
    listeners.add(listener);
}

In your touch event

for (ViewWasTouchedListener listener:listeners){
   listener.onViewTouched(x);
}

In your Activity:

public class Test extends Activity implements ViewWasTouchedListener {

...

@Override
public void onCreate(Bundle savedInstanceState) 
{
    ...
    MyExtendedView customView = (MyExtendedView)findViewById(R.id.myCustomeView);
    customView.setWasTouchedListener(this);
    ...
}

public void onViewTouched(float x){
   // do whatever you need to do
}

All from memory so please excuse typos and you should improve the view class by adding removeViewWasTouchedListener and checking that you do not add the same listener twice in setViewWasTouchedListener.

You probably want y as well so just add it to the Interface definition.

You should override onTouch() Try this,

   @Overrride
   public boolean onTouch(View v, MotionEvent event) {

   int xPoint = event.getX();
   int yPoint = event.getY();
   return true;
   }

After getting some help and changing some things I've got my code to do what I wanted to do:

My Activity:

 public class TargetPractice extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            final View viewMain; 

            setContentView((viewMain = View.inflate(this, R.layout.main, null))); 

            new MyImageView(this, viewMain,
                            new TargetInterface() {

                                    @Override
                                    public void OnTouchEventDown(float x, float y) {                                                
                                            Log.w("MYPRG", String.format("%f, %f", x, y));
                                    }
                            });
    }        
 }

My Custom Class:

  public class MyImageView extends View implements View.OnTouchListener {

    Context context;

            private TargetInterface iTarget = null;

            public float x=0;
            public float y=0;                       

            public float getx() { 
              return this.x;
                             }

            public MyImageView(Context context, View view, TargetInterface iTarget) 
            {
                            super(context);
                            this.context=context;                           
                            this.iTarget=iTarget;   // bind TargetInterface

                            setFocusable(true);
                            view.setOnTouchListener(this);
            }               

            @Override               
            public boolean onTouchEvent(MotionEvent event)  
            {
                    switch (event.getAction())
                    {
                              case MotionEvent.ACTION_DOWN: 
                              {
                                            x = event.getX();
                                            y = event.getY();



                                            return true;
                              }                               
                    }

               return false;
            }
                @Override
                public void onDraw(Canvas canvas){
                super.onDraw(canvas);

                 ....
              }
          public boolean onTouch(View v, MotionEvent event) {
              x = event.getX();
              y = event.getY();
             iTarget.OnTouchEventDown(x, y);
            return false;
}                               
   }

My interface :

    public interface TargetInterface {

          public void OnTouchEventDown(float x, float y);

    }   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM