简体   繁体   English

将文本从一种积极性转移到另一项活动

[英]Get Text from one Acitivity to another Activity

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? 我必须进行“ Main”和“ Shop”活动,并且在“ Shop”中有一个变量,我想在“ Main” -Activity中使用该变量,我该怎么做? Thanks for help. 感谢帮助。 I would like to have the intCountValue2 in my "Main" class. 我想在我的“ Main”类中添加intCountValue2。 That is my whole Shop code: 那是我的整个商店代码:

@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();
        }
    });
}

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;

Before starting Android Programming, you must have a good knowledge about Object Oriented Programming. 在开始Android编程之前,您必须对面向对象编程有充分的了解。 However, to solve your problem you may try to use Getter and Setter method concept (In this particular case, only getter is needed). 但是,要解决您的问题,您可以尝试使用Getter and Setter method概念(在这种情况下,只需要使用getter)。 To do this in your particular case: 为此,请在您的特定情况下:

  • 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 of Shop class, inside the setOnClickListener of btnBuyClickMultiplier , set the value of count to intCountValue2 like this: 现在,在Shop类的onCreate()方法中,在setOnClickListenerbtnBuyClickMultiplier ,将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 of Shop class created inside Main class like this: 最后,您可以从在Main类内部创建的Shop类的实例访问intCountValue2变量的值,如下所示:

     Shop s = new Shop(); int intCountvalue2 = s.getCount(); 

Hope you understand. 希望你能理解。

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

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