I have to activitys, "Main" and "Shop" and i got a variable in "Shop" that i would like to use in my "Main"-Activity, how can i do that? Thanks for help. I would like to have the intCountValue2 in my "Main" class. That is my whole Shop code:
我必须活动,“主要”和“商店”,我在“商店”中得到一个变量,我想在我的“主要” - 活动中使用,我该怎么做?感谢帮助。我想在我的“Main”类中使用intCountValue2。这是我的整个商店代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_shop);
btnBuyClickMultiplier = (Button) findViewById(R.id.ClickMultiplierButton);
txtMultiplierCounter= (TextView) findViewById(R.id.ClickMultiplier);
btnBuyClickMultiplier.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String CountValue2= txtMultiplierCounter.getText().toString();
int intCountValue2 = Integer.parseInt(CountValue2);
intCountValue2++;
txtMultiplierCounter.setText(String.valueOf(intCountValue2));
}
});
configureBackButton1();
}
private void configureBackButton1() {
Button BackButton1 = (Button) findViewById(R.id.BackButton1);
BackButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
2 个解决方案
#1
1
The easisest way to do it but not the best is to declare your variable "shop" as public and static like the following:
最简单的方法是将你的变量“shop”声明为public和static,如下所示:
public static String shop;
And then you can just call that variable in your Main activity as the following:
然后,您可以在Main活动中调用该变量,如下所示:
ShopActivity.shop;
#2
1
Before starting Android Programming, you must have a good knowledge about Object Oriented Programming. However, to solve your problem you may try to use Getter and Setter method
concept (In this particular case, only getter is needed). To do this in your particular case:
在开始Android编程之前,您必须对面向对象编程有很好的了解。但是,要解决您的问题,您可以尝试使用Getter和Setter方法概念(在这种特殊情况下,只需要getter)。要在您的特定情况下执行此操作:
-
At first, in your
Shop
class declare a private variable (obviously outside any methods, like-onCreate()
or so) like this:首先,在你的Shop类中声明一个私有变量(显然在任何方法之外,比如onCreate()等),如下所示:
private int count;
-
Then, create a public method in
Shop
class like this:然后,在Shop类中创建一个公共方法,如下所示:
public int getCount() { return this.count; }
-
Now, in your
onCreate()
method ofShop
class, inside thesetOnClickListener
ofbtnBuyClickMultiplier
, set the value ofcount
tointCountValue2
like this:现在,在Shop类的onCreate()方法中,在btnBuyClickMultiplier的setOnClickListener中,将count的值设置为intCountValue2,如下所示:
btnBuyClickMultiplier.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String CountValue2= txtMultiplierCounter.getText().toString(); int intCountValue2 = Integer.parseInt(CountValue2); intCountValue2++; txtMultiplierCounter.setText(String.valueOf(intCountValue2)); count = intCountValue2; } });
-
Finally, you can access the value of
intCountValue2
variable from the instance ofShop
class created insideMain
class like this:最后,您可以从Main类中创建的Shop类的实例访问intCountValue2变量的值,如下所示:
Shop s = new Shop(); int intCountvalue2 = s.getCount();
Hope you understand.
希望你能理解。
#1
1
The easisest way to do it but not the best is to declare your variable "shop" as public and static like the following:
最简单的方法是将你的变量“shop”声明为public和static,如下所示:
public static String shop;
And then you can just call that variable in your Main activity as the following:
然后,您可以在Main活动中调用该变量,如下所示:
ShopActivity.shop;
#2
1
Before starting Android Programming, you must have a good knowledge about Object Oriented Programming. However, to solve your problem you may try to use Getter and Setter method
concept (In this particular case, only getter is needed). To do this in your particular case:
在开始Android编程之前,您必须对面向对象编程有很好的了解。但是,要解决您的问题,您可以尝试使用Getter和Setter方法概念(在这种特殊情况下,只需要getter)。要在您的特定情况下执行此操作:
-
At first, in your
Shop
class declare a private variable (obviously outside any methods, like-onCreate()
or so) like this:首先,在你的Shop类中声明一个私有变量(显然在任何方法之外,比如onCreate()等),如下所示:
private int count;
-
Then, create a public method in
Shop
class like this:然后,在Shop类中创建一个公共方法,如下所示:
public int getCount() { return this.count; }
-
Now, in your
onCreate()
method ofShop
class, inside thesetOnClickListener
ofbtnBuyClickMultiplier
, set the value ofcount
tointCountValue2
like this:现在,在Shop类的onCreate()方法中,在btnBuyClickMultiplier的setOnClickListener中,将count的值设置为intCountValue2,如下所示:
btnBuyClickMultiplier.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String CountValue2= txtMultiplierCounter.getText().toString(); int intCountValue2 = Integer.parseInt(CountValue2); intCountValue2++; txtMultiplierCounter.setText(String.valueOf(intCountValue2)); count = intCountValue2; } });
-
Finally, you can access the value of
intCountValue2
variable from the instance ofShop
class created insideMain
class like this:最后,您可以从Main类中创建的Shop类的实例访问intCountValue2变量的值,如下所示:
Shop s = new Shop(); int intCountvalue2 = s.getCount();
Hope you understand.
希望你能理解。