[英]how to use same string in two java files
Sorry for my bad English and for maybe stupid question but I'm new in Java. 对不起,我的英语不好,也许还有愚蠢的问题,但我是Java新手。
I need use same string in 2 java files for example: 我需要在2个Java文件中使用相同的字符串,例如:
In first java file I've got code for sending emails, I've got string set to default email: 在第一个Java文件中,我有用于发送电子邮件的代码,我已将字符串设置为默认电子邮件:
public String mail = new String ("lala@gmail.com");
and I use this string in code for send email: 我在代码中使用此字符串发送电子邮件:
email.addTo(mail);
In second java file something like set up where can user set new email address I want to have same string, connected with string in first java file. 在第二个Java文件中,例如设置,用户可以在其中设置新的电子邮件地址,我想拥有相同的字符串,并与第一个Java文件中的字符串连接。 When user put new email String mail will be change to new email address and in email.addTo(mail);
当用户输入新电子邮件时,字符串邮件将更改为新电子邮件地址,并在email.addTo(mail);中。 will be use this new address
将使用这个新地址
How can I do this? 我怎样才能做到这一点?
use Shared Preferences, you can store it as key-value Pair. 使用共享首选项,您可以将其存储为键值对。 value being your email and key can be any unique string which you want to identify it with.
值是您的电子邮件和密钥,可以是您要用来标识它的任何唯一字符串。
I'm a bit confused with the question, but I'll take a stab at it. 我对这个问题有些困惑,但是我会对此加以刺探。 Basically, you would like to have one String in a given file be used in multiple locations.
基本上,您希望在给定文件中的一个字符串在多个位置使用。 This is easily done using class-level variables and making them publicly accessible.
使用类级变量并使它们可以公共访问很容易做到。
For example, in the file: 例如,在文件中:
EmailObject.java EmailObject.java
public class EmailObject {
public static final String mail = "lala@gmail.com";
// The rest of your code
}
Another file can access this like so: 另一个文件可以这样访问:
OtherObject.java OtherObject.java
public void sendEmail() {
EmailMessage email = new EmailMessage();
email.addTo(EmailObject.mail);
}
Note the static
and final
modifiers on the original. 注意原始文件上的
static
和final
修饰符。 This ensures that you do not need an actual instance of EmailObject to access the string and it also ensures that the string is never modified accidentally by some other object. 这样可以确保您不需要实际的EmailObject实例即可访问该字符串,还可以确保该字符串永远不会被其他对象意外修改。
There are, of course, other ways to do this, but this one matches your code the most. 当然,还有其他方法可以执行此操作,但是这种方法与您的代码最匹配。 This is also a very "Java" solution.
这也是一个非常“ Java”的解决方案。 Android has other ways to share data (as indicated by the other answer).
Android还有其他共享数据的方式(如另一个答案所示)。
The simplest way that I would not recommend is to have a public static field: 我不建议使用的最简单方法是拥有一个公共静态字段:
class A {
public static String commonString;
}
class B {
public void methodThatUsesString () {
// Do stuff with the string
Log.d("I have the string", A.commonString);
}
}
If you have two Activities, and one starts another, you can send data through Intents. 如果您有两个活动,一个活动又开始另一个活动,则可以通过Intent发送数据。
The forementioned SharedPreferences way is a good solution too, if the email address is a persistent thing, a preference if you will, and not just data reqired for an operation. 前面提到的SharedPreferences方式也是一个很好的解决方案,如果电子邮件地址是永久性的,则可以选择首选项,而不仅仅是操作所需的数据。
You can keep a reference of one instance of a class in the otherone, and access it's fields through it: 您可以在另一个类中保留对一个类的一个实例的引用,并通过它访问其字段:
class A {
public String commonString;
}
class B {
private final A instaceOfA;
public B (A instanceOfA) {
this.instanceOfA = instanceOfA;
}
public void methodThatUsesString () {
// Do stuff with the string
Log.d("I have the string", instanceOfA.commonString);
}
}
Or even use a getter or setter if performance is not an issue. 如果性能不成问题,甚至可以使用吸气剂或吸气剂。
Many answers depending on how the string will be used. 许多答案取决于如何使用字符串。
If it's a constant string, one that will never change, never use final static String 如果它是一个常量字符串,则永远不变,永远不要使用最终的静态字符串
public final static String AUTHOR_MAIL = "lala@gmail.com"; 公共最终静态字符串AUTHOR_MAIL =“ lala@gmail.com”;
Then you can use it in a static way wherever you want. 然后,您可以在任何地方以静态方式使用它。
email.addTo(MyClass.AUTHOR_MAIL); email.addTo(MyClass.AUTHOR_MAIL);
If this String will be used in different Activities you can not access it directly (you can not tell if the other Activity is still alive). 如果此字符串将在其他活动中使用,则您将无法直接访问它(无法确定其他活动是否仍然存在)。 You have to use Persistence Mechanisms such as
SharedPreferences
or directly send needed data in your Intent. 您必须使用诸如
SharedPreferences
持久性机制,或者直接在Intent中发送所需的数据。
If it's in a helper class inside your Activity, you can just use mObject.mail to get it. 如果它在Activity的帮助器类中,则可以使用mObject.mail来获取它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.