如何通过向 Intent 传递一些参数来启动它? [英] How to start an Intent by passing some parameters to it?

查看:26
本文介绍了如何通过向 Intent 传递一些参数来启动它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ListActivity 的构造函数中传递一些变量

I would like to pass some variables in the constructor of my ListActivity

我通过此代码开始活动:

I start activity via this code:

startActivity(new Intent (this, viewContacts.class));

我想使用类似的代码,但要向构造函数传递两个字符串.怎么可能?

I would like to use similar code, but to pass two strings to the constructor. How is possible?

推荐答案

为了传递参数,你创建了新的 Intent 并放置了一个参数映射:

In order to pass the parameters you create new intent and put a parameter map:

Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("firstKeyName","FirstKeyValue");
myIntent.putExtra("secondKeyName","SecondKeyValue");
startActivity(myIntent);

为了获取已启动活动中的参数值,您必须在同一意图上调用 get[type]Extra():

In order to get the parameters values inside the started activity, you must call the get[type]Extra() on the same intent:

// getIntent() is a method from the started activity
Intent myIntent = getIntent(); // gets the previously created intent
String firstKeyName = myIntent.getStringExtra("firstKeyName"); // will return "FirstKeyValue"
String secondKeyName= myIntent.getStringExtra("secondKeyName"); // will return "SecondKeyValue"

如果您的参数是整数,您将使用 getIntExtra() 代替等.现在您可以像往常一样使用您的参数.

If your parameters are ints you would use getIntExtra() instead etc. Now you can use your parameters like you normally would.

这篇关于如何通过向 Intent 传递一些参数来启动它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆