Android - 将 Java 类导入 Activity
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27309852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Import Java Class into Activity
提问by
I have a java class called Constants
filled with variables. And currently if I want to use that variable in an activity I have to use it like this.
我有一个名为Constants
充满变量的 java 类。目前,如果我想在活动中使用该变量,我必须像这样使用它。
Constants.variable = "blah blah";
Is there a way to import my Constants
class into my activity so that I can just use it like a normal variable like this
有没有办法将我的Constants
类导入到我的活动中,以便我可以像这样使用普通变量
variable = "blah blah";
I have tried
我试过了
import com.myname.appname.Constants;
but this doesn't seem to work.
但这似乎不起作用。
Thanks for the help in advance.
我在这里先向您的帮助表示感谢。
回答by tachyonflux
try:
尝试:
import com.myname.appname.Constants;
Are you using an IDE?
你用的是IDE吗?
Also, they way you have stated is how you are suppose to use a Constants class. But you are not suppose to assign anything... because the variable are suppose to be constant, ie. static final
此外,您所说的方式是您假设如何使用 Constants 类。但是你不应该分配任何东西......因为变量应该是常量,即。静态最终
回答by Blaze Tama
Is there a way to import my constants class into my activity so that I can just use it like a normal variable like this
有没有办法将我的常量类导入到我的活动中,这样我就可以像这样像普通变量一样使用它
I dont think so, its not what import
for.
我不这么认为,这不是import
为了什么。
See this SO question : Meaning of the import statement in a Java file
请参阅此问题:Java 文件中导入语句的含义
回答by Munawwar Hussain Shelia
considering Constants.variableis a static variable
考虑Constants.variable是一个静态变量
import static com.myname.appname.Constant.*;
this will import all you static variables in you current namespace only the static variable
这将导入当前命名空间中的所有静态变量,只有静态变量
import static com.myname.appname.Constant.variable;
now you can use variable like a normal variable
现在你可以像普通变量一样使用变量
回答by jafarbtech
You have to declare "variable" as static and import the class as static by following syntax
您必须通过以下语法将“变量”声明为静态并将类导入为静态
import static com.myname.appname.Constants.*;