Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

How to pass a value from one Activity to another in Android?

I have created an Activity with a AutuCompleteTextView[ACTV] and button. I enter some text in the ACTV then press the button. After I press the button I want the Activity to go to another Activity. In the second Activity I just want to display the text entered in ACTV(of the first actvity) as a TextView.

I know how to start the second activity which is as below:

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

I have coded this to obtain the text entered from the ACTV.

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
CharSequence getrec=textView.getText();

My question here is how to pass "getrec" (after I press the button) from the first Activity to the second. And later recieve "getrec" in the second activity.

Please assume that I have created the event handler class for the button by using "onClick(View v)"

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Bundle to do the same in Android

Create the intent:

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);

Now in your second activity retrieve your data from the bundle:

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String stuff = bundle.getString(“stuff”); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...