简体   繁体   English

如何在Java中跨多个类使用变量?

[英]How to use a variable across multiple classes in java?

I am trying to create my first app in android studio, on the main screen there are three tick boxes asking the user which number of sides they want on the dice. 我正在尝试在android studio中创建我的第一个应用程序,在主屏幕上有三个刻度框,询问用户他们想要在骰子上的哪几个面。 I have a variable called sides which is set to 6,8 or 12 depending on which tick box the user ticks. 我有一个名为sides的变量,它根据用户打勾的复选框设置为6,8或12。 I want the variable "sides" on the second activity so it can be used to generate a random integer between one and whatever "sides" is set to. 我希望在第二个活动中使用变量“ sides”,以便可以使用它在一个和设置为“ sides”的变量之间生成随机整数。

In first activity Lets assume that you have button GO . 在第一个活动中,假设您具有按钮GO。 When You clicks on Button GO it should start Second Activity say Activity2. 当您单击Button GO时,它应该启动Second Activity,说Activity2。 Add following code to onClick of GO Button 将以下代码添加到GO按钮的onClick

Intent act2=new Intent(this,Activity2.class);//"this" is  activity reference
act2.putExtra("key",value);
startActivity(act2);

Now in the onCreate method of Activity2 you can retrieve value of key as follows: 现在,在Activity2的onCreate方法中,您可以按以下方式检索键的值:

Int key=getIntent().getIntExtra("key",0);//0 is default value

In the same way as done above you can pass value of "side" variable to next activity 与上述操作相同,您可以将“ side”变量的值传递给下一个活动

You can also save it in the Internal Storage and load it when you need, it is very useful because that way you can load it in every activity and every class you want. 您还可以将其保存在内部存储器中,并在需要时加载它,这非常有用,因为这样您就可以在每个活动和所需的每个类中加载它。

you can learn how here . 您可以在这里学习如何。 I recommend watching all three parts. 我建议看全部三个部分。

you can use SharedPreferences, 您可以使用SharedPreferences,

To obtain shared preferences, use the following method In your activity: 要获取共享的首选项,请在您的活动中使用以下方法:

SharedPreferences prefs = this.getSharedPreferences(
  "com.example.app", Context.MODE_PRIVATE);

To read preferences: 要阅读首选项:

String dateTimeKey = "com.example.app.datetime";

// use a default value using new Date()
long l = prefs.getLong(dateTimeKey, new Date().getTime()); 

To edit and save preferences 编辑和保存首选项

Date dt = getSomeDate();
prefs.edit().putLong(dateTimeKey, dt.getTime()).apply();

The android sdk's sample directory contains an example of retrieving and storing shared preferences. android sdk的示例目录包含一个检索和存储共享首选项的示例。 Its located in the: 其位于:

<android-sdk-home>/samples/android-<platformversion>/ApiDemos directory

Easiest way is to use singleton classes. 最简单的方法是使用单例类。

public class DataHolder {


public int sides  = 0;

    private static DataHolder dataHolder = new DataHolder();

    public static DataHolder getInstance()
    {
        return dataHolder;
    }


} 

DataHolder.getInstance().sides=sideInActivityA;

you can access the variable by using int sideInActivityB = DataHolder.getInstance().sides; 您可以使用int sideInActivityB = DataHolder.getInstance().sides;访问变量int sideInActivityB = DataHolder.getInstance().sides;

This can be done by static global variables or the public variables which can be accessed by inheriting the class. 这可以通过静态全局变量或可以通过继承该类的公共变量来完成。 However importing the global static classes does the same. 但是,导入全局静态类的功能相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM