Anyone who has worked in Asp.net or other web development knows that to convert between two webpages, you only need to use hyperlinks. But in mobile phones, how can we convert between mobile phone pages? The simplest way is to change the layout of the activity. First, prepare the two layout files main. axml and layout2.axml. Place a button in layout1. When you click it, layout2 is displayed. Similarly, a button is also placed in layout2. When you click it, return to main.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview Android: text = "this is layout1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> <br/> <button Android: text = "Jump to layout2" <br/> Android: layout_y = "30px" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/btn1"/> <br/> </absolutelayout> <br/>
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: Background = "@ color/White"> <br/> <textview Android: text = "this is layout2" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"/> <br/> <button Android: text = "Jump to layout1" <br/> Android: layout_y = "30px" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/btn2"/> <br/> </absolutelayout>
Then add the following code to the oncreate method in activity1.cs:
Using system; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> using Android. OS; <br/> using monodroidtest. tabs; <br/> using Android. util; <br/> using Java. io; <br/> using Android. database; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "monodroidtest", mainlauncher = true)] <br/> public class activity1: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> button btn1 = findviewbyid <button> (resource. id. btn1); <br/> btn1.click + = (sender, e) => <br/>{< br/> This. setcontentview (resource. layout. activity2); <br/> button btn2 = findviewbyid <button> (resource. id. btn2); <br/> btn2.click + = (sender2, E2) => <br/>{< br/> oncreate (bundle); <br/> }; <br/>}; <br/>}< br/>}
By using the layout technique to change the activity, you can make the page conversion effect on the mobile phone. Of course, you can also use the style settings described earlier for more flexible layout configuration. Besides, using setcontentview to replace the page also has a special advantage, that is, all the variables in the program are in the same State, whether it is class members, class methods, etc, are directly obtained in the status of an activity, and there is no problem of parameter passing. For example, for a page that requires step-by-step navigation, the information obtained from the first page does not need to be transferred to the second page, but is stored in the members of the current activity and can be used at any time.
If the page to be converted is not only a different background, color, or text content, but a replacement of activity, it is not just a change of layout. In particular, the data to be transmitted is not implemented by the session or cache on the web page, but the master control is transferred to another activity in the program, which cannot be implemented simply by the previous layout technique.
So how can we solve the transfer of activity control? In monodroid, you can use the startactivity method in the main program to call another activity (the main program itself is an activity), but the key is not the startactivity method, it is the unique object of intent. Intent is like an English word, meaning "want" or "intention". In the main activity, it tells the program what it is and where it wants to go, this is what the intent object handles.
The layout file is the same as the previous one. The difference is that the program code is used to modify the activity. CS is as follows. Here we will teach you another method for binding control events. net program, we need to bind the event, as long as the event name + = event processing object or Lambda expression, such as BTN. click + = (sender, e) =>{}; In monodroid, in order to be consistent with the writing method in Java, in addition to the preceding method, another method is provided, that is, the setonxxxlistener method. For example, in the Click Event of a button, setonclicklistener is called. The input parameter is a class that implements view. ionclicklistener. In addition, to implement the Java excuse, your class must inherit the java. Lang. object class, so that you do not need to implement the iobject handle attribute.
Using system; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> using Android. OS; <br/> using monodroidtest. tabs; <br/> using Android. util; <br/> using Java. io; <br/> using Android. database; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "monodroidtest", mainlauncher = true)] <br/> public class activity1: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> button BTN = findviewbyid <button> (resource. id. btn1); <br/> BTN. setonclicklistener (New clicklistener (); <br/>}< br/> public class clicklistener: Java. lang. object, view. ionclicklistener <br/>{< br/> Public void onclick (view v) <br/>{< br/> Activity Act = v. context as activity; <br/> intent = new intent (); <br/> intent. setclass (Act, typeof (activity2); <br/> act. startactivity (intent); <br/> act. finish (); <br/>}< br/>
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. OS; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "My activity")] <br/> public class activity2: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. activity2); <br/> button BTN = findviewbyid <button> (resource. id. btn2); <br/> BTN. click + = DeleGate <br/>{< br/> intent = new intent (); <br/> intent. setclass (this, typeof (activity1); <br/> This. startactivity (intent); <br/> This. finish (); <br/>}; <br/>}< br/>}
The finish () method is called in both the activity1 and activity2 classes. It indicates that the activity has been completed. When the system receives this command, the activity is closed, therefore, you can click the back key of the simulator to return to the screen of the previous activity. To enable the back key of the simulator to return to the previous page, you can comment out this line of code. Similarly, when two acitifiers are switching, they do not really only have two activities to switch, but re-call a new activity when they click the button.
If you need to transfer data while calling another activity, you need to use the bundle object to encapsulate data. Use bundle to transmit data or parameters that you want to pass between different intents.
When activity1 calls activity2, upload a string and an integer to activity2. add several lines of code to The onclick method of the clicklistener class:
Public void onclick (view v) <br/>{< br/> Activity Act = v. context as activity; <br/> intent = new intent (); <br/> intent. setclass (Act, typeof (activity2); <br/> bundle B = new bundle (); <br/> B. putstring ("name", "ojlovecd"); <br/> B. putint ("score", 76195); <br/> intent. putextras (B); <br/> act. startactivity (intent); <br/> act. finish (); <br/>}< br/>
Then, the two values are received in activity2. To bring up a prompt after activity2 is loaded, we first encapsulate an information box prompt class. The alertdialog window is used here. It is often used for program prompts, warnings, and confirmation.
Using system; <br/> using Android. APP; <br/> using Android. content; <br/> namespace monodroidtest <br/>{< br/> public class MessageBox <br/>{< br/> Public static void show (context CTX, String title, string message) <br/>{< br/> alertdialog. builder DLG = new alertdialog. builder (CTX); <br/> DLG. settitle (title); <br/> DLG. setmessage (Message); <br/> DLG. setpositivebutton ("OK", delegate {}); <br/> DLG. show (); <br/>}< br/> Public static void showerrormessage (context CTX, exception ex) <br/>{< br/> show (CTX, "error", Ex. message); <br/>}< br/>}
Then we use this class in activity2 to display the parameters passed from activity1:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. OS; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "My activity")] <br/> public class activity2: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. activity2); <br/> bundle B = This. intent. extras; <br/> button BTN = findviewbyid <button> (resource. id. btn2); <br/> BTN. click + = DeleGate <br/>{< br/> intent = new intent (); <br/> intent. setclass (this, typeof (activity1); <br/> This. startactivity (intent); <br/> This. finish (); <br/>}; <br/> MessageBox. show (this, "info", String. format ("username: {0}/N points: {1}", B. getstring ("name"), B. getint ("score"); <br/>}< br/>}
Running result:
The bundle object provides multiple methods for different data types. For example, putstring is used to transmit data of the string type, and putdouble is used to transmit data of the double type. Otherwise, to retrieve data from the bundle object, change put to get. I tried to pass non-basic data, that is, custom data, But no matter whether my object is Java. io. iserializable, then call the putserializable method, or implement iparcelable. If you call the putparcelable method, an error will be reported during the get operation. If a friend can successfully pass custom data, share it with you.
In the previous example, it is difficult to pass data from activity1 to activity2. If you want to return to activity1, the data will not be encapsulated again? In addition, the previous activity1 has been destroy for a long time. If you end the program with finish () at the end of activity1, use activity2 to bundle the data and pass the parameters through activity1, this method can restore user input data, but it does not meet our expectation, especially the data that the user has input. If you accidentally click back to the previous page, the data disappears.
If you want to add a "back-to-page" button on the second page, instead of using the simulator's return key, and the previous information can be retained after going back to the page, the startactivityforresult method must be used to evoke another activity. To process the result returned by activity2, you must re-write the onactivityresult method in activity1. The program is as follows:
Using system; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> using Android. OS; <br/> using monodroidtest. tabs; <br/> using Android. util; <br/> using Java. io; <br/> using Android. database; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "monodroidtest", mainlauncher = true)] <br/> public class activity1: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> button BTN = findviewbyid <button> (resource. id. btn1); <br/> BTN. setonclicklistener (New clicklistener (); <br/>}</P> <p> protected override void onactivityresult (INT requestcode, result resultcode, intent data) <br/>{< br/> If (resultcode = result. OK) <br/> {<br/> bundle B = data. extras; <br/> string name = B. getstring ("name"); <br/> int score = B. getint ("score"); <br/> string blogurl = B. getstring ("blogurl"); <br/> int availablescore = B. getint ("availablescore"); <br/> MessageBox. show (this, "prompt", String. format ("Name: {0}/N points: {1}/n blog address: {2}/N available score: {3}", name, score, blogurl, availablescore); <br/>}< br/> public class clicklistener: Java. lang. object, view. ionclicklistener <br/>{< br/> Public void onclick (view V) <br/>{< br/> // try <br/> // {<br/> Activity Act = v. context as activity; <br/> intent = new intent (); <br/> intent. setclass (Act, typeof (activity2); <br/> bundle B = new bundle (); <br/> B. putstring ("name", "ojlovecd"); <br/> B. putint ("score", 76195); <br/> intent. putextras (B); <br/> act. startactivityforresult (intent, 0); <br/> // act. startactivity (intent); <br/> // act. finish (); <br/>/}< br/> // catch (exception ex) <br/> // {<br/> // MessageBox. show (v. context, "error", Ex. message); <br/>/}< br/>}< br/>
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. OS; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "My activity")] <br/> public class activity2: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. activity2); <br/> bundle B = This. intent. extras; <br/> // create your application here <br/> button BTN = findviewbyid <button> (resource. id. btn2); <br/> BTN. click + = DeleGate <br/>{< br/> intent = new intent (); <br/> B. putstring ("blogurl", "http://blog.csdn.net/ojlovecd"); <br/> B. putint ("availablescore", 18734); <br/> intent. putextras (B); <br/> This. setresult (result. OK, intent); <br/> This. finish (); <br/>}; <br/> MessageBox. show (this, "info", String. format ("username: {0}/N points: {1}", B. getstring ("name"), B. getint ("score"); <br/>}< br/>}
Running result:
In this example, startactivity can achieve the same effect. You only need to judge whether there is any data in intent when activity1 is created, if not, null is input. However, the program still needs to compare with or without values, which is complicated. Since monodroid API provides a better method, why not? What's more, if the system does not only have a few lines but hundreds or thousands of lines, then it would not be too big?