Android uses the special intent class to implement switching between activity and activity. By parsing various intents, it is easy to navigate from one screen to another. When you navigate forward, the activity will call
Startactivity (intentmyintent) method.
The intenttest. Java code is as follows:
1. package intent. pack; <br/> 2. <br/> 3. import android. app. activity; <br/> 4. import android. content. intent; <br/> 5. import android. OS. bundle; <br/> 6. import android. view. view; <br/> 7. import android. view. view. onclicklistener; <br/> 8. import android. widget. button; <br/> 9. import android. widget. edittext; <br/> 10. <br/> 11. public class intenttest extends activity {<br/> 12. /** called when the activity is first created. */<br/> 13. private edittext ET1; <br/> 14. @ override <br/> 15. public void oncreate (bundle savedinstancestate) {<br/> 16. super. oncreate (savedinstancestate); <br/> 17. setcontentview (R. layout. main); <br/> 18. ET1 = (edittext) findviewbyid (R. id. edittext1); <br/> 19. button bt = (button) findviewbyid (R. id. button1); <br/> 20. BT. setonclicklistener (New buttonlistener (); <br/> 21 .} <br/> 22. class buttonlistener implements onclicklistener {<br/> 23. @ override <br/> 24. public void onclick (view arg0) {<br/> 25. string val = et1.gettext (). tostring (); <br/> 26. // generate an intent object <br/> 27. intent intent = new intent (); <br/> 28. // set the passed parameter <br/> 29. intent. putextra ("Val", Val); <br/> 30. // jump from activity intenttest to activity intenttest01 <br/> 31. intent. setclass (intenttest. this, intenttest01.class); <br/> 32. // start intent activity <br/> 33. intenttest. this. startactivity (intent); <br/> 34 .} <br/> 35. <br/> 36 .} <br/> 37 .}
The intenttest01.java code is as follows:
1. package intent. pack; <br/> 2. <br/> 3. import android. app. activity; <br/> 4. import android. content. intent; <br/> 5. import android. OS. bundle; <br/> 6. import android. widget. textview; <br/> 7. <br/> 8. public class intenttest01 extends activity {<br/> 9. @ override <br/> 10. public void oncreate (bundle savedinstancestate) {<br/> 11. super. oncreate (savedinstancestate); <br/> 12. setcontentview (R. layout. intent01); <br/> 13. intent intent = getintent (); <br/> 14. string value = intent. getstringextra ("Val"); <br/> 15. textview ET = (textview) findviewbyid (R. id. textview1); <br/> 16. ET. settext (value); <br/> 17 .} <br/> 18 .} <br/>
The main. XML Code is as follows:
1. <? XML version = "1.0" encoding = "UTF-8"?> <Br/> 2. <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> 3. android: Orientation = "vertical" <br/> 4. android: layout_width = "fill_parent" <br/> 5. android: layout_height = "fill_parent" <br/> 6.> <br/> 7. <edittext <br/> 8. android: Id = "@ + ID/edittext1" <br/> 9. android: layout_width = "match_parent" <br/> 10. android: text = "Enter the content! "<Br/> 11. android: layout_height = "wrap_content"> <br/> 12. </edittext> <br/> 13. <button <br/> 14. android: Id = "@ + ID/button1" <br/> 15. android: text = "OK" <br/> 16. android: layout_height = "wrap_content" <br/> 17. android: layout_width = "match_parent"> <br/> 18. </button> <br/> 19. </linearlayout> <br/>
The intent01.xml code is as follows:
<Br/> 1. <? XML version = "1.0" encoding = "UTF-8"?> <Br/> 2. <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> 3. android: Orientation = "vertical" <br/> 4. android: layout_width = "fill_parent" <br/> 5. android: layout_height = "fill_parent" <br/> 6.> <br/> 7. <textview <br/> 8. android: Id = "@ + ID/textview1" <br/> 9. android: textsize = "20sp" <br/> 10. android: layout_width = "match_parent" <br/> 11. android: text = "Enter the content! "<Br/> 12. Android: layout_height =" wrap_content "> <br/> 13. </textview> <br/> 14. </linearlayout> <br/>
The androidmanifest. XML Code is as follows:
<Br/> 1. <? XML version = "1.0" encoding = "UTF-8"?> <Br/> 2. <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> 3. package = "intent. pack "<br/> 4. android: versioncode = "1" <br/> 5. android: versionname = "1.0"> <br/> 6. <uses-SDK Android: minsdkversion = "4"/> <br/> 7. <br/> 8. <application <br/> 9. android: icon = "@ drawable/icon" <br/> 10. android: Label = "@ string/app_name"> <br/> 11. <activity Android: Name = ". intenttest "<br/> 12. android: Label = "@ string/app_name"> <br/> 13. <intent-filter> <br/> 14. <action Android: Name = "android. intent. action. main "/> <br/> 15. <category Android: Name = "android. intent. category. launcher "/> <br/> 16. </intent-filter> <br/> 17. </activity> <br/> 18. <activity Android: Name = ". intenttest01 "<br/> 19. android: Label = "@ string/app1_name"> <br/> 20. </activity> <br/> 21. </Application> <br/> 22. </manifest> <br/>