【问题标题】:Android: How to pass the data to sub-activities?Android:如何将数据传递给子活动?
【发布时间】:2010-11-07 13:41:29
【问题描述】:

主要活动包括一些具有设定值的变量。我使用表单创建了一个子活动,该表单必须填充来自主活动的数据,所以我猜数据必须在启动时传递给子活动。

有谁知道如何将变量值从主活动传递给子活动?

谢谢!

【问题讨论】:

    标签: android android-activity


    【解决方案1】:

    你可以在你的主要活动中使用这个方法

    Intent i = new Intent(this, YourMainClass.class);
    i.putExtra("key", value);
    

    然后在子activity中用这个方法获取值,一般在onCreate事件中

    int value = getIntent().getExtras().getInt("key");
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这会在主要活动中起作用吗?

      Intent i = new Intent(this, YourMainClass.class);
      i.putExtra("key", value);
      

      接着是:

      String value = getIntent().getExtras().getString("key");
      

      你可以添加多个“附加”或类似的东西吗?

      i.putExtra("key", value1); 
      i.putExtra("key2", value2);
      i.putExtra("key3", value3);
      

      谢谢...

      【讨论】:

        【解决方案3】:

        试试这个,它会起作用的:

        activity1.class:

        Intent i = new Intent(activity1.this,activity2.class);
        
        Bundle b = new Bundle();
        b.putString("name", "your value need to pass here");
        
        i.putExtras(b);
        startActivity(i);
        

        activity2.class:

        Bundle b = this.getIntent().getExtras();
        
        String name = b.getString("name");
        
        ((TextView)findViewById(R.id.textView1)).setText(name);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-10-30
          • 1970-01-01
          • 2014-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多