Li huaming himiOriginal, reprinted must be explicitly noted:
Reprinted from[Heimi gamedev block]Link: http://www.himigame.com/android-game/301.html
Many kids shoes say that after my code is run, clicking home or back will cause a program exception. If you have encountered this, you certainly haven't carefully read the himi blog, in article 19th, himi specifically wrote about the causes and solutions of these errors. I have added my remarks on this blog, and my provincial children's shoes are always confused. Please click here to contact us for further reading:
[Android game development 19th] (required) surfaceview Running Mechanism explanation-analyze back and home buttons and switch to the background to handle exceptions!
Today, I will explain how to implement switching between two or more activities and data interaction in surfaceview. I have provided a demo of the game logon interface to improve my image, I also wrote the layout of the input interface at will, mainly because the function does not take time to beautify, so you can change the layout in XML or code by yourself, the width and height of each widget. Okay, next (I love it ~ Doraemon, so I have to use the figure of Doraemon, Wahaha)
Because there are three classes in the Code, it is not convenient to paste them all here; you can download the source code at the end of the article;
There are two steps to switch between two activities:
1. Implement code switching. 2. Declare another acitivity in the configuration!
Let's take a look at the first step: Here is a piece of code in the touch screen processing:
Public Boolean ontouchevent (motionevent event) {<br/> float pointx = event. getx (); <br/> float pointy = event. gety (); <br/> If (pointx> bp_x + 14 & pointx <bp_x + 14 + 117) {<br/> If (pointy> bp_y + 43 & pointy <bp_y + 43 + 15) {<br/> // account <br/> intent I = new intent (); // get an instance with an intent <br/> I. putextra ("count", 1); // write data <br/> I. putextra ("himi", str_zh); <br/> I. setclass (mainactivity. instance, register. class); // set the current activity and the class to be operated <br/> mainactivity. instance. startactivity (I); // use the current activity to start another activity <br/>}< br/>}
Display and define an intent object. The mechanism of the intent class is to assist in interaction. The detailed description is not described here;
The putextra () function in intent interacts with two activities. This method is the same as put in hashtable or hashmap. The first parameter is the key (index ), the latter parameter Volue (value). Based on the key, we can get the corresponding Volue. Then I will also attach the accepted processing.
The setclass () function in intent is also used to input two parameters. The first parameter is the activity object of the current instance. The next parameter is the activity class to be opened! Then we can use the current activity object to start another activity. Then let's take a look at how another activity creates and accepts data.
/** <Br/> */<br/> package COM. himi; <br/> Import android. app. activity; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. edittext; <br/> Import android. widget. linearlayout; <br/> Import android. widget. textview; <br/>/** <br/> * @ author him I <br/> */<br/> public class register extends activity {<br/> private button button_ OK; <br/> private edittext et; <br/> private textview TV; <br/> private linearlayout ly; <br/> private register RS; <br/> private byte count; <br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> rs = This; <br/> LY = new linearlayout (this); <br/> Bu Tton_ OK = new button (this); <br/> button_ OK .setwidth (100); <br/> button_ OK .settext ("OK"); <br/> button_ OK .setonclicklistener (New onclicklistener () {<br/> Public void onclick (view v) {<br/> If (COUNT = 1) {<br/> mysurfaceview. str_zh = et. gettext (). tostring (); <br/>}else if (COUNT = 2) {<br/> mysurfaceview. str_pass = et. gettext (). tostring (); <br/>}< br/> Rs. finish (); <br/>}< br/>}); <br/> intent inten T = This. getintent (); <br/> COUNT = (byte) intent. getintextra ("count", 0); <br/> string temp_str = ""; <br/> string temp_str2 = ""; <br/> ET = new edittext (this); <br/> TV = new textview (this); <br/> If (count! = 3) {<br/> temp_str = intent. getstringextra ("himi"); <br/> If (COUNT = 1) {<br/> Rs. settitle ("enter an account! "); <Br/>}else {<br/> Rs. settitle (" enter the password! "); <Br/>}< br/> LY. addview (TV); <br/> LY. addview (ET); <br/> LY. addview (button_ OK); <br/> If (temp_str! = NULL) {<br/> ET. settext (temp_str); <br/>}< br/>} else {<br/> temp_str = intent. getstringextra ("himi_zh"); <br/> temp_str2 = intent. getstringextra ("himi_pass"); <br/> Rs. settitle ("your input information:"); <br/> TV. settext ("account:" + temp_str + "/N" + "password" + temp_str2); <br/> LY. addview (TV); <br/> LY. addview (button_ OK); <br/> If (temp_str! = NULL) {<br/> ET. settext (temp_str); <br/>}< br/> setcontentview (ly); <br/>}< br/>
The code above shows that to create an activity, you only need to inherit the activity and override the oncreate () method. Of course, the creation still requires a very important step. In the second step, we will explain in detail how to accept data from previous activities ,.
Intent intent = This. getintent ();
Count = (byte) intent. getintextra ("count", 0 );
The acceptance is also very concise and easy to understand. Create an intent object and call the getintextra function to obtain the previous data, according to the key! Of course, the getstringextra () and other functions are similar, but different functions are selected based on the data you input. The children's shoes should pay attention to what the second parameter in getintextra means. In fact, when the key cannot be found to match, it will return 0 by default;
Next we will introduce Step 2: declare in configuration
When creating an activity, we must live the class we created in androidmainfeset. xml. It is an activity! The code in XML is attached below!
<Activity Android: Name = "com. himi. register "Android: theme =" @ Android: style/theme. dialog "<br/> Android: screenorientation =" Landscape "Android: configchanges =" keyboardhidden | orientation "> <br/> </activity> <br/> <! -- I am a comment --> <br/> <activity Android: Name = "com. himi. Register"> </activity>
<! ----> This symbol is a comment
Here we have two declaration Methods Separated by comments. The preceding Declaration also sets some attributes and theme display formats, in fact, the simplest statement is also possible, just as the statement below can be used.
Make sure to declare a new activity. Do not forget this step.
Note: There is a property in the configuration-> Android: screenorientation this is to set the horizontal screen, so clicking on the vertical screen will have a problem. If you want to display the portrait screen normally, you can delete android in XML by yourself: screenorientation = "Landscape.
(We recommend that you subscribe to this blog, because our update speed is very fast ~ Wahaha)
Source code: http://www.himigame.com/android-game/301.html