1: Code Implementation switching operation
2: Another acitivity is declared in the configuration
Let's look at the first step, here is a piece of code in the touch-screen process:
The code is as follows |
Copy Code |
public boolean ontouchevent (Motionevent event) { float Pointx = Event.getx (); float pointy = event.gety (); if (Pointx > bp_x + && pointx < bp_x + 14 + 117) { if (Pointy > bp_y + && Pointy < bp_y + 43 + 15) { Account Intent i = new Intent ();//Get an instance of intent I.putextra ("Count", 1);/write Data I.putextra ("Himi", Str_zh); I.setclass (mainactivity.instance, Register.class)//Set the current activity and the class that will be manipulated MainActivity.instance.startActivity (i);//Start another activity with current activity } } } |
Shows the definition of a intent object, intent the mechanism of this class is to facilitate interaction, detailed instructions here is not much to say.
The Putextra () function in intent acts as an interactive interaction between two activity, similar to the put in Hashtable or HashMap, the first parameter is key (index), and the latter parameter is Volue (value). According to Key we can get the corresponding volue. Then I also enclose the accepted processing.
The SetClass () function in Intent also passes in two parameters, the first is the activity object passed in to the current instance, and the following is the class of activity that needs to be opened! We can then use the current Activity object to start another activity. Then we'll look at how the other activity is created and how it accepts the data.
The code is as follows |
Copy Code |
Package Com.himi; 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.LinearLayout; Import Android.widget.TextView; /** * @author Himi **/ public class Register extends activity { Private Button BUTTON_OK; Private EditText et; Private TextView TV; Private LinearLayout ly; Private Register RS; private byte Count; @Override protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); rs = this; ly = new LinearLayout (this); BUTTON_OK = new Button (this); Button_ok.setwidth (100); Button_ok.settext ("OK"); Button_ok.setonclicklistener (New Onclicklistener () { public void OnClick (View v) { if (count = = 1) { Mysurfaceview.str_zh = Et.gettext (). toString (); } else if (count = = 2) { Mysurfaceview.str_pass = Et.gettext (). toString (); } Rs.finish (); } }); Intent Intent = This.getintent (); Count = (byte) Intent.getintextra ("Count", 0); String temp_str = ""; String temp_str2 = ""; ET = new EditText (this); TV = new TextView (this); if (Count!= 3) { Temp_str = Intent.getstringextra ("Himi"); if (count = = 1) { Rs.settitle ("Please enter account number!"); } else { Rs.settitle ("Please enter password!"); } Ly.addview (TV); Ly.addview (ET); Ly.addview (BUTTON_OK); if (temp_str!= null) { Et.settext (TEMP_STR); } } else { Temp_str = Intent.getstringextra ("Himi_zh"); TEMP_STR2 = Intent.getstringextra ("Himi_pass"); Rs.settitle ("The information you entered:"); Tv.settext ("Account number:" + Temp_str + "n" + "password" + temp_str2); Ly.addview (TV); Ly.addview (BUTTON_OK); if (temp_str!= null) { Et.settext (TEMP_STR); } } Setcontentview (ly); } } |
As you can see from the above code, creating a new activity requires only inheriting the activity and overriding the OnCreate () method. Of course, it takes a very important step to create, and I'll elaborate on that in the second step, and here we'll look at how to accept the data from the previous activity.
The code is as follows |
Copy Code |
Intent Intent = This.getintent (); Count = (byte) Intent.getintextra ("Count", 0);
|
Accept is also very simple and understandable, create a intent intent object, invoke the Getintextra function to get the data from before, according to key. Of course, there are Getstringextra () and so on functions are similar, but according to the data you pass in different choose different functions. Students should pay attention to the second parameter in the Getintextra is what meaning, in fact, is a key to find a match when the default return 0;
So here's the second step: declaring in the configuration
When creating an activity we have to declare in Androidmainfeset.xml that the class we created is an activity.