Android Studio ---- foundation to learn to use Intent to pass information between Activity

Learn to use Android Studio Intent to pass information between Activity

Chinese eat the whole game because of a wise child off at home, Shashi Hou out to play? Maybe when eating chicken rice, dog licking finished surface, burning off the lock ... "Do not dwell on out to play, it also busy enough chaos," my mother there, saying
all right! In this waiting time caring about learning! Wuhan Come on! !

In the Android software, we often click on a button to jump to another page, or click on the jump, for example: when buying train tickets, click on the "seat selection" to enter the page to select a seat, click on " rush tickets "that is another page, and maybe someone will say, I ordered what" rush tickets "," seat selection "Han Han is not a button Yeah, word or whatever.
In practice, this has a number of settings related to the style, but then, it was the truth that reason, are equally applicable.
Here's a brief look at how to use the Intent to pass information between Activity

White Zimo, there are problems to discuss with everyone made it!

Intent used to pass information between Activity

1, first create a new Activity, page design in activity_main.xml, the android.support.constraint.ConstraintLayout changed LinearLayout (linear mode), modified tail will be automatically changed to LinearLayout, give a direction android: orientation = " vertical ", delete the original TextView. Add TextView, button and other controls. Figure

Here Insert Picture DescriptionHere Insert Picture Description

2, binding MainActivity the id of this control, monitor and set the click event of the button, when you set a button click event, we achieved two pages jump, the main intent achieved through to complete two pages Jump, we must first create a new activity, to bind these two pages by intent.Here Insert Picture Description

## new changes
3、要实现两个页面的跳转,我们要在Java中新建一个BattleActivity,并设置它的界面,添加控件。
Here Insert Picture Description
4、同样在BattleActivity里面我们也要先设置一下界面 ,添加按钮等控件。
Here Insert Picture Description

5、要实现两个页面的传递,需要调用startActivityForResult(Intent,requestCode)函数,参数intent用于决定启动哪个Activity,参数requestCode是请求码,因为所有被调用的(子)Activity返回时,调用的(父)Activity都要调用相同的处理函数,因此父Activity使用RequestCode来确定数据是哪一个子Activity返回的。
在这里我将训练页面的RequestCode设为1,将商店页面的RequestCode设为2 。
Here Insert Picture Description```
6、置子Activity的返回值,在子Activity调用finish()函数关闭前,调用setRequest()函数设定需要返回给父Activity的数据,setRequest()函数有两个参数,一个是结果码,一个是返回值。结果码表示子Activity的返回状态,可自定义结果码。Activity返回值封装在Intet中,则子Activity通过intent将需要返回的数据传递给父Activity。Here Insert Picture Description
## generates a list for you
7、在父Activity中获取返回值,当子Activity关闭后,父Activity会调用onActivityResult()函数,用来获取子Activity的返回值,onActivityResult()有3个参数,其中第一个参数是请求码,用来判断第3个参数是哪一个子Activity的返回值;resultCode用于表示子Activity的数据返回状态;data是子Activity的返回数据,返回数据类型是intent。
After the request code is determined, and the corresponding event request code provided by the switch function. Case1 execution BattleActivity in the appropriate thing, executing the appropriate thing in shopActivity Case2 time. The same method button controls BattleActivity and shopActivity page set.
Here Insert Picture Description

Results Figure

home pageClick "pk" to enter the pageClick to enter the store page

Information is displayed after clicking the "Getting Started training" or buy "clothing", etc.
White practice, please advise!

Published an original article · won praise 0 · Views 18

Guess you like

Origin blog.csdn.net/Genus_/article/details/104254916