如何从同一Android项目的另一个文件夹中的另一个类中访问变量?

huangapple 未分类评论42阅读模式
英文:

How to access variables from another class that are in another folder of the same android project?

问题

我通过触摸屏事件使用了一个屏幕设计库这个设计的结果就是我需要传递给另一个类的坐标即同一项目中的ActivityMain但位于另一个文件夹中我如何访问从我使用的库中得到的这些值下面是我在Android上组织项目的方式的图片该库位于"library"中的Java语言中我需要访问默认文件夹中的主要活动中的变量该文件夹位于我的项目中的Kotlin语言中

Gradle应用

implementation project(':library')

这些变量是

public class ShapeDrawingActivity extends AppCompatActivity {
    public float startX;
    public float startY;
    public float finalX;
    public float finalY;
    Log.i("value ", shape.startX.toString())
}

退出日志 = 1162.4032

MainActivity

import com.editing.canvas.library.ShapeDrawingActivity

class MainActivity : AppCompatActivity() {
    private val shape: ShapeDrawingActivity = ShapeDrawingActivity()

    Log.i("test return", shape.startX.toString())
}

退出日志 = 0.0
英文:

I'm using a screen design lib through the touch screen event, the result of this design, which is the coordinates I need to pass to another class, the ActivityMain of the same project, but in another folder. How do I access these values ​​that are coming from the lib I'm using. Below is the image of how my project is organized on android. The lib is in the java language inside "library" and I need to access the variables inside my mainactivity in the default folder of my project, which is in the kotlin language.

如何从同一Android项目的另一个文件夹中的另一个类中访问变量?

Gradle app:

implementation project(':library')

The variables are these:

public class ShapeDrawingActivity extends AppCompatActivity {

    public float startX;
    public float startY;
    public float finalX;
    public float finalY;
    Log.i("value ", shape.startX.toString())
}

Exit log = 1162.4032

MainActivity:

import com.editing.canvas.library.ShapeDrawingActivity

class MainActivity : AppCompatActivity() {
    private val shape: ShapeDrawingActivity = ShapeDrawingActivity()

    Log.i("test return", shape.startX.toString())
}

Exit Log = 0.0

答案1

得分: 0

首先,在您的 Gradle 中初始化此模块,根据您的情况,它是一个库,并在 Gradle 文件中添加此模块的依赖项。
然后调用该类,
Android 会自动将模块导入到您的包中,您就可以获取变量。

val shapeDrawingActivity: ShapeDrawingActivity = ShapeDrawingActivity()
val startX = ShapeDrawingActivity.startX
英文:

First you initialize this module in your gradle in your case its a library and add dependencies of this module in gradle file.
Then Call the class
android automatically import the module in your package and you get the variables.

ShapeDrawingActivity:ShapeDrawingActivity = ShapeDrawingActivity()
ShapeDrawingActivity.startX

答案2

得分: 0

是的。您可以使用静态变量,但请确保在任务完成后需要释放静态变量,因为静态变量在整个项目中存在,即使您更改活动或类也是如此。

public class ShapeDrawingActivity extends AppCompatActivity {
    
    public static float startX;
    public static float startY;
    public static float finalX;
    public static float finalY;
    Log.i("value", siz.startX.toString())
}

在不同的类中可以像这样使用:

class MainActivity : AppCompatActivity() {
    
    Log.i("test return", ShapeDrawingActivity.startX.toString())
}
英文:

Yes. You can use using static variable, but make sure after completion of the task you need to deallocate static variable because static variable lives throughout the project even if you change activity or class.

public class ShapeDrawingActivity extends AppCompatActivity {

public static float startX;
public static float startY;
public static  float finalX;
public static  float finalY;
Log.i("value ", siz.startX.toString())
}

and in the different class use like this

class MainActivity : AppCompatActivity(){

        Log.i("test return", ShapeDrawingActivity.startX.toString())
}

huangapple
  • 本文由 发表于 2020年4月4日 07:18:03
  • 转载请务必保留本文链接:https://java.coder-hub.com/61021801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定