Android how to pass a variable to another java file
In Reminder1.java I开发者_如何学Go have the int hourOfDay2 and int minute2 variables. These equals with the hourOfDay and minute variable of the TimePickerDialog. In myfile.java i want to examine the value of these variables. How to do that?
One thing I've seen posted here on SO a few times, and that I've used for global variables, is an extended Application class, like so:
public class GlobalVars extends Application {
private static int hourOfDay2;
private static int minute2;
public static int getHourOfDay() {
return hourOfDay2;
}
public static int getMinute() {
return minute2;
}
public static void setHourOfDay(int hour) {
hourOfDay2 = hour;
}
public static void setMinute(int minute) {
minute2 = minute;
}
}
Add it to your Application tag in the manifest, like so:
<application android:name=".GlobalVars" />
Then, in your main class's onCreate, or wherever necessary, just call GlobalVars.setMinute(int)
to initialize them, then you can access them the same way in any other class, with int x = GlobalVars.getMinute()
.
In android, you can use Bundle to pass values.
精彩评论