Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to resolve the problem of clicking a button to start another Activity and passing values between Activity in Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to analyze the problem of clicking a button to start another Activity in Android and the problem of passing values between Activity. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Scene

Click the button in the first Activity to start the second Activity, close the second Activity, and return to the first Activity.

The value is passed to the second Activity in the first Activity, and it is fetched and displayed in the second Activity.

Open the second Activity

Activity value

Realize

Start another Activity

In the click event of the button in the first Activity

Button secondActivityButton = (Button) findViewById (R.id.secondActivity); secondActivityButton.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / the first parameter is the current Activity; the second parameter is the Activity Intent intent = new Intent (MainActivity.this,SecondActivity.class); startActivity (intent);}})

Then in the click event of the close button in the second Activity

Button closeButton = (Button) findViewById (R.id.closeButton); closeButton.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {finish ();}})

Start Activity and pass a value

In the first Activity button click event

Button paramActivityButton = (Button) findViewById (R.id.paramActivity); paramActivityButton.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (MainActivity.this, SecondActivity.class); / / data intent.putExtra in the form of multiple key-value pairs ("key", "domineering rogue temperament"); intent.putExtra ("key1", "overbearing programmer"); startActivity (intent);}})

In the button click event in the second Activity

Button valueButton = (Button) findViewById (R.id.valueButton); valueButton.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {String valueString = (String) getIntent (). GetExtras (). Get ("key"); String valueString1 = (String) getIntent (). GetExtras (). Get ("key1"); Toast.makeText (SecondActivity.this, "the first parameter obtained is:" + valueString+ "the second parameter is:" + valueString1,Toast.LENGTH_LONG). Show ();})

The above content is how to analyze the problem of clicking a button to start another Activity and passing values between Activity in Android. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report