【问题标题】:get Integer variable from other activity [closed]从其他活动中获取整数变量 [关闭]
【发布时间】:2014-11-09 15:44:31
【问题描述】:
    button1.setOnClickListener(new OnClickListener()
    {

        public void onClick(View v)
        {

            numberPicker1int = Integer.parseInt(numberPicker1.getContext().toString());
            numberPicker2int = Integer.parseInt(numberPicker2.getContext().toString());
            value1=numberPicker1int;
            value2=numberPicker2int;
   }
        });

value1 和 value2 在其他活动(MainActivity)中。我如何获得 value1(int) 和 value2(int)?

【问题讨论】:

标签: java android eclipse integer


【解决方案1】:

在主要活动中:

Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("value1",value1); intent.putExtra("value2",value2); startActivity(intent);

在您想要访问这些值的辅助活动中:

    Bundle bundle = getIntent().getExtras();
    int value1 = bundle.getInt("value1");
    int value2 = bundle.getInt("value2");

【讨论】:

    【解决方案2】:

    如果我正确理解了您的问题,那么您可以声明两个实例变量:

    Private value1 As Integer
    Private value2 As Integer
    

    然后你可以创建两个属性来获取值。

    【讨论】:

      【解决方案3】:

      在开始您的其他活动时,只需在捆绑中传递值

      Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
      int myValue1 = 23213;
      int myValue2 = 21223;
      Bundle b = new Bundle();
      b.putInt("value1", myValue1);
      b.putInt("value2", myValue2);
      intent.putExtras(b);
      startActivity(intent);
      

      在您的第二个活动中,您会获得以下值:

      Bundle b = getIntent().getExtras();
      int myValue1 = b.getInt("value1", 0);
      int myValue2 = b.getInt("value2", 0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-22
        • 2019-10-26
        相关资源
        最近更新 更多