Android:在类之间传递参数

标签 android parameters android-intent

我有一个class2,当点击时它被class1 所涉及。我必须将一些参数/对象从 class1 传递到 class2。我只知道没有参数传递选项的标准方式。

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

startActivity(i);

最佳答案

您可以使用 Intent.putExtra(使用 Bundle)传递额外数据。

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

然后,一旦您进入新的 Activity:

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

这允许您将基本数据传递给 Activity 。但是,您可能需要做更多的工作才能传递任意对象。

关于Android:在类之间传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2582690/

相关文章:

android - 三星S3颜色问题

c - C 中的 main 函数有 void 和没有 void

php - mysql_close() 期望参数 1 为资源,给定为空

java - 获取空对象 - 将数据从一个 Activity 发送到另一个 Activity

php - 如何制作json api

android - ScrollView如何滚动到想要的位置?

android - BroadcastReceiver 的 onReceive 中 Intent extra 为空

Android 选择 Intent

android - 适用于 Android 的动画 GIF 编码器

C++构造函数默认参数