. Net programmers play Android Development --- (11) page Jump,. netandroid
In any program development process, page jumps occur, and Android development is no exception. this section describes how to redirect pages in the Android project. page jumps include page jumps with or without parameters, and the return values of another page have been accepted. In Android, Intent is commonly used for page Jump, But Intent not only performs page Jump, but also can do other things, such as dialing, sending text messages, and calling other programs. This section describes how to use Intent to redirect pages.
1. page Jump
First, let's take a look at Intent. Intent has several overloaded constructors. We use one of the constructors for page Jump.
Intent intent = new Intent (MainActivity. this, SecondActivity. class); there are two parameters. The first parameter represents the current page object, and the second parameter represents the target object to jump.
After the Intent is created, we use startActivity to start it. ,
Let's take a look at this effect.
Launch page
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/plugin" tools: context = ". mainActivity "> <Button android: id =" @ + id/button1 "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: layout_alignParentTop =" true "android: layout_centerHorizontal = "true" android: layout_marginTop = "74dp" android: text = "jump to the next page"/> </RelativeLayout>
Package com. example. hellotest; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity {private Button btn; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btn = (Button) findViewById (R. id. button1); btn. setOnClickListener (new OnClickListener () // bind Registration button click event {@ Override public void onClick (View arg0) {// button to jump to Intent intent = new Intent (MainActivity. this, SecondActivity. class); startActivity (intent) ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Target page
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/textView1" android: layout_width = "219dp" android: layout_height = "wrap_content" android: layout_weight = "0.19" android: text = "second page"/> </LinearLayout>
2. Page jump with Parameters
In this example, let's take A look at page Jump and pass value. First, page A jumps to page B and passes the value. Then page B returns page A and transmits the value to page. As follows:
Send content to page B
Click return to page A and pass the input content to page.
A page layout and code
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <LinearLayout android: layout_width = "fill_parent" android: layout_weight = "0.9" android: layout_height = "fill_parent"> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_weight = "0.1" android: orientation = "vertical" android: layout_height = "fill_parent"> <LinearLayout android: layout_marginLeft = "10px" android: layout_marginRight = "10px" android: gravity = "center" android: layout_width = "fill_parent" android: orientation = "horizontal" android: layout_height = "wrap_content"> <TextView android: textSize = "8pt" android: text = "sent content:" android: id = "@ + id/tvSend" android: layout_weight = "0.7" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> </TextView> <EditText android: layout_weight = "0.3" android: layout_width = "fill_parent" android: text = "" android: id = "@ + id/etmsg" android: layout_height = "wrap_content"> </EditText> </LinearLayout> <LinearLayout android: layout_marginLeft = "10px" android: layout_marginRight = "10px" android: gravity = "center" android: layout_width = "fill_parent" android: orientation = "horizontal" android: layout_height = "wrap_content"> <Button android: text = "send" android: textSize = "9pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: id = "@ + id/btnSend"> </Button> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_weight = "0.9" android: layout_height = "fill_parent"> <TextView android: textSize = "8pt" android: text = "Accept returned content:" android: id = "@ + id/tvreturn" android: layout_weight = "0.7" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> </TextView> </LinearLayout>
Package com. example. hellotest; import android. app. activity; import android. content. intent; import android. content. sharedPreferences; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; public class OrginLayOut extends Activity {private Button btn; // send Button private EditText edtiText; // send content private TextView tvReturn; // accept the content displayed on the subpage returned by protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. orginlayout); btn = (Button) findViewById (R. id. btnSend); // get the button object edtiText = (EditText) findViewById (R. id. etmsg); // get the text box object tvReturn = (TextView) findViewById (R. id. tvreturn); btn. setOnClickListener (new OnClickListener () // bind Registration button click event {@ Override public void onClick (View arg0) {// button to jump to Intent intent Intent = new intent (); Intent. setClass (OrginLayOut. this, TargetActivity. class); Bundle bundle = new Bundle (); bundle. putString ("msg", edtiText. getText (). toString (); // transmits the value intent. putExtras (bundle); // startActivity (intent); startActivityForResult (intent, 0 );}});} // accept the return value of the page @ Override // requestCode Request ID // resultCode returns the ID protected void onActivityResult (int requestCode, int resultCode, Intent data) {if (requestCode = 0) {if (resultCode = Activity. RESULT_ OK) {String content = data. getStringExtra ("returnmsg"); tvReturn. setText ("Accept returned content:" + content );}}}}
B page layout and code
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <LinearLayout android: layout_width = "fill_parent" android: layout_weight = "0.9" android: layout_height = "fill_parent"> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_weight = "0.1" android: orientation = "vertical" android: layout_height = "fill_parent"> <LinearLayout android: layout_marginLeft = "10px" android: layout_marginRight = "10px" android: gravity = "center" android: layout_width = "fill_parent" android: orientation = "horizontal" android: layout_height = "wrap_content"> <TextView android: textSize = "8pt" android: text = "content accepted:" android: id = "@ + id/tvreceivemsg" android: layout_weight = "0.7" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> </TextView> </LinearLayout> <LinearLayout android: layout_marginLeft = "10px" android: layout_marginRight = "10px" android: gravity = "center" android: layout_width = "fill_parent" android: orientation = "horizontal" android: layout_height = "wrap_content"> <TextView android: textSize = "8pt" android: text = "Returned content:" android: layout_weight = "0.7" android: layout_width = "fill_parent" android: layout_height = "wrap_content"> </TextView> <EditText android: layout_weight = "0.3" android: layout_width = "fill_parent" android: text = "" android: id = "@ + id/etreturnmsg" android: layout_height = "wrap_content"> </EditText> </LinearLayout> <LinearLayout android: layout_marginLeft = "10px" android: layout_marginRight = "10px" android: gravity = "center" android: layout_width = "fill_parent" android: orientation = "horizontal" android: layout_height = "wrap_content"> <Button android: text = "return" android: textSize = "9pt" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: id = "@ + id/btnreturn"> </Button> </LinearLayout>
Package com. example. hellotest; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; public class TargetActivity extends Activity {private TextView TV; private Button btn; private EditText returnText; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView(R.layout.tar getlayout); TV = (TextView) findViewById (R. id. tvreceivemsg); btn = (Button) findViewById (R. id. btnreturn); // get the button object returnText = (EditText) findViewById (R. id. etreturnmsg); // get the Bundle bunde of the text box object = this. getIntent (). getExtras (); String strs = "accept content:" + bunde. getString ("msg "). toString (); TV. setText (strs); btn. setOnClickListener (new OnClickListener () // bind Registration button click event {@ Override public void onClick (View arg0) {// button to jump to Intent data = new Intent (); data. putExtra ("returnmsg", returnText. getText (). toString (); setResult (Activity. RESULT_ OK, data); finish ();}});}}
Here are a few simple methods:
StartActivityForResult if the parent page wants to accept the return value of the Child page, use this method to subscribe the page. The first parameter of the method indicates the information of the Child page to be started, and the second parameter indicates the request code,
Is an integer. The Request Code requestCode indicates that if a child page has multiple parent pages that can be started, the request code can be used to determine the parent page from.
OnActivityResult (int requestCode, int resultCode, Intent data) is used to receive the content returned by the parent page. The first parameter is the request code, and the second parameter is the resultCode of the returned result, resultCode is mainly used to determine which subpage to return. The third parameter is the returned data.
SetResult (Activity. RESULT_ OK, data); this method is mainly used for subpages. The first parameter is the return identifier, and the second parameter is the returned data.
Download: http://download.csdn.net/detail/zx13525079024/8136253