我使用AutuCompleteTextView [ACTV]和按钮创建了一个Activity。我在ACTV输入一些文字,然后按下按钮。 按下按钮后,我希望活动转到另一个活动。在第二个Activity中我只想将在ACTV(第一次动作中)输入的文本显示为TextView。
我知道如何开始第二项活动,如下所示:
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);
我已对此进行编码,以获取从ACTV输入的文字。
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
CharSequence getrec=textView.getText();
我的问题是如何将“getrec”(在我按下按钮后)从第一个Activity传递到第二个Activity。后来在第二次活动中收到“getrec”。
请假设我已使用“onClick(View v)”为该按钮创建了事件处理程序类
答案 0 :(得分:56)
你可以使用Bundle在Android中做同样的事情
创建意图:
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“stuff”, getrec);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
现在,在您的第二个活动中,从捆绑包中检索您的数据:
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String stuff = bundle.getString(“stuff”);
答案 1 :(得分:6)
将数据从一个活动传递到另一个活动的标准方式:
如果要将大量数据从一个活动发送到另一个活动,那么您可以将数据放入一个包中,然后使用putExtra()
方法传递它。
//Create the `intent`
Intent i = new Intent(this, ActivityTwo.class);
String one="xxxxxxxxxxxxxxx";
String two="xxxxxxxxxxxxxxxxxxxxx";
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“ONE”, one);
bundle.putString(“TWO”, two);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
否则,您可以直接使用putExtra()
发送数据,getExtra()
来获取数据。
Intent i=new Intent(this, ActivityTwo.class);
i.putExtra("One",one);
i.putExtra("Two",two);
startActivity(i);
答案 2 :(得分:2)
多一点,使用Intent.putExtra将数据传递给您开始的活动。 然后使用Bundle.getExtra来检索它。
已经有很多这样的问题https://stackoverflow.com/search?q=How+to+pass+a+value+from+one+Activity+to+another+in+Android 请务必在下次使用搜索。
答案 3 :(得分:1)
以这种方式实施
String i="hi";
Intent i = new Intent(this, ActivityTwo.class);
//Create the bundle
Bundle b = new Bundle();
//Add your data to bundle
b.putString(“stuff”, i);
i.putExtras(b);
startActivity(i);
在此activity
内开始第二个class
以利用Bundle值使用此代码
Bundle bundle = getIntent().getExtras();
String text= bundle.getString("stuff");
答案 4 :(得分:0)
简单如果你将字符串X从A传递给B.
的 A - > B
在活动A中
1)创建意图
2)使用putExtra意图方法将数据置于意图中
3)开始活动
Intent i = new Intent(A.this, B.class);
i.putExtra("MY_kEY",X);
在活动B中
在onCreate方法中
1)获取意图对象
2)使用密钥(MY_KEY)
Intent intent = getIntent();
String result = intent.getStringExtra("MY_KEY");
这是将数据从A发送到B的标准方式。 你可以发送任何数据类型,它可以是int,boolean,ArrayList,String []。根据您作为键存储在Activity中的数据类型,值对检索方法可能会有所不同,如果您传递的是int值,那么您将调用
intent.getIntExtra("KEY");
您甚至可以发送Class对象,但为此,您必须使您的类对象实现Serializable或Parceable接口。
您可以跨大小发送多少数据。 如果数据大小超过一定数量,那么您可能会得到TransactionTooLargeException。 假设您尝试在活动中发送位图,如果大小超过某个数据大小,那么您可能会看到此异常。
答案 5 :(得分:0)
在第一个活动中:
Intent i=new Intent(getApplicationContext,secondActivity.class);
i.putExtra("key",value);
startActivity(i);
,并在SecondActivity中:
String value=getIntent.getStringExtra("Key");
答案 6 :(得分:0)
示例Kotlin代码如下:-
第1页
val i = Intent(this, Page2::class.java)
val getrec = list[position].promotion_id
val bundle = Bundle()
bundle.putString("stuff", getrec)
i.putExtras(bundle)
startActivity(i)
第2页
var bundle = getIntent().getExtras()
var stuff = bundle.getString("stuff")
答案 7 :(得分:-1)
我知道这已经很晚了,但是如果需要跨多个意图访问和修改变量,最简单的方法是使用单例方式。 定义一个全局变量,可以访问所有意图。