简体   繁体   English

如何使用setter和getter在Android的类之间传递数据

[英]How to use setters and getters to pass data between classes in android

I'm writing a program that has two classes, one that extends Activity and another that extends SurfaceView. 我正在编写一个包含两个类的程序,一个扩展了Activity,另一个扩展了SurfaceView。 The activity has an object of the SurfaceView class. 该活动具有SurfaceView类的对象。 I am trying to use setters and getters to send data between these two classes, but every time I try, eclipse says that the methods for setting and getting need to be static. 我试图使用setter和getter在这两个类之间发送数据,但是每次尝试时,eclipse都说设置和获取方法必须是静态的。 I can't do this because I don't want them to be static. 我不能这样做,因为我不希望它们是静态的。

The Activity class contains the following methods: Activity类包含以下方法:

public float getxTouch(){
return xTouch;
}
public float getyTouch(){
return yTouch;
}

The SufaceView class contains the following code: SufaceView类包含以下代码:

xpos = ActivityClass.getxTouch();
ypos = ActivityClass.getyTouch();

How might I fix this without making the methods static? 如何在不使方法静态化的情况下解决此问题?

You can use Intents to transfer references of variables between your Activity and your class. 您可以使用Intent在您的Activity和您的类之间传递变量的引用。

First, let's create a serializable class that will contain your variables: 首先,让我们创建一个可序列化的类,其中将包含您的变量:

class XYTouch implements Serializable{
  public static final String EXTRA = "com.your.package.XYTOUCH_EXTRA";

  private float xTouch;

  public void setX(String x) {
      this.xTouch = x;
  }

  public String getX() {
      return xTouch;
  }

// do the same for yTouch    
}

Then, in your activity's onCreate , create a new XYTouch object and set its xTouch and yTouch attributes using set and get methods. 然后,在您活动的onCreate ,创建一个新的XYTouch对象,并使用set和get方法设置其xTouch和yTouch属性。 Then write 然后写

Intent intent = new Intent(this, OtherClass.class);
intent.putExtra(XYTouch.EXTRA, xytouchobject);
startActivity(intent);

In your other class (OtherClass) in which you want access to those variables: 在要访问这些变量的其他类(OtherClass)中:

public void onCreate(Bundle savedInstance){
   // ....
   XYTouch xytouch = (XYTouch) getIntent().getSerializableExtra(XYTouch.EXTRA);
   // ....
}

Then, you can use get and set methods of XYTouch anywhere in your class to have a reference to xTouch and yTouch. 然后,您可以在类中的任何位置使用XYTouch的get和set方法来引用xTouch和yTouch。

Another way would be to retrieve it from a class of your own that extends Application and keeps a reference to those variables. 另一种方法是从您自己的扩展Application的类中检索它,并保留对这些变量的引用。 Then you would use an Activity context to read them in. 然后,您将使用“活动”上下文读取它们。

Pass the reference of the activity class to your surface class. 将活动类的引用传递给您的表面类。 Do something like this, so you don't have to make the methods static. 做这样的事情,所以您不必使方法静态。

In your SurfaceView class: 在您的SurfaceView类中:

Activity foo;

//some method to set foo:

public void setFoo(Activity foo) {
this.foo = foo;
}

// Then you can simple call your getX() and getY() methods like this:

foo.getX(); and foo.getY();

From your Activity class: 从您的活动课:

yourSurfaceViewInstance.setFoo(this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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