我现在在 Android Studio 工作,但我对整个编程概念有点陌生。我知道如何在 Matlab 中编码,您可以在其中创建函数并在代码中调用它们,因此每个任务都有不同的文件。这里有类(class),但我不太明白。我有代码可以生成不重复的随机数,但我将该代码作为 MainActivity.java 中的函数。我需要在项目中多次使用该代码。我是否必须使用该函数创建一个新文件并从代码中调用它?或者只是将该函数留在 MainActivity.java 中?
如果我用英语犯了一些错误或者问题太愚蠢,抱歉。
最佳答案
do I have to create a new file with the function and call it from within the code? or just let the function inside MainActivity.Java?
您不需要创建新文件!在 MainActivity.Java 中编写您的函数,如下所示:
private int yourRandomNumberMethod(){//
// your code
}
然后在您的 Activity 中通过 onCreate() 方法轻松调用它。
另外我建议你更多地了解JAVA和Android开发!
编辑:
I need to use that code several times in the project. Do I have to create a new file with the function and call it from within the code
对于这项工作,创建一个新类并将您的代码放在这里,在您需要函数的地方从该类创建一个实例,并使用该实例调用您的方法!
关于java - 如何在Android Studio中管理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28679690/