安卓:类之间传递参数参数

2023-09-12 09:28:39 作者:埋没我的痴狂

我有一个类2 这是涉及由 1级点击后制成。我要通过一些参数/从 1级对象为类2 。我只知道它不具有传递参数选项的标准方法。

  //启动全文
意图I =新的意图(这一点,Class2.class);

startActivity(ⅰ);
 

解决方案

您可以使用 Intent.putExtra (它使用捆绑)来传递额外的数据。

 意向书我=新的意图(这一点,Class2.class);
i.putExtra(富,5.0F);
i.putExtra(酒吧,巴兹);
startActivity(ⅰ);
 
2. python与C之间参数传递与接收,指针类型的传入接收,以及结构体,联合体

然后,一旦你是新的活动里面

 捆绑额外= getIntent()getExtras()。
如果(临时演员!= NULL)
{
 浮富= extras.getFloat(富);
 串吧= extras.getString(巴);
}
 

这可以让您将基础数据的活动。但是,您可能需要沿着传递任意对象多一点的工作。

I have a class2 which is involved by class1 when clicks are made. I have to pass some parameters/objects from class1 to class2. I only know the standard way which does not have an option of passing parameters.

// launch the full article
Intent i = new Intent(this, Class2.class);

startActivity(i);

解决方案

You can use Intent.putExtra (Which uses a Bundle) to pass extra data.

Intent i = new Intent(this, Class2.class);
i.putExtra("foo", 5.0f);
i.putExtra("bar", "baz");
startActivity(i);

Then once you're inside your new Activity:

Bundle extras = getIntent().getExtras(); 
if(extras !=null)
{
 float foo = extras.getFloat("foo");
 String bar = extras.getString("bar");
}

This allows you to pass basic data to Activities. However, you may need a bit more work for passing arbitrary objects along.

 
精彩推荐