"Android" uses bundles to pass values between multiple activity

Source: Internet
Author: User
Tags gettext

Wirelessly activity is equivalent to a form in vb,mfc, and passing data between multiple activity is a pretty core function. Here's an example to illustrate the problem.


I. BASIC OBJECTIVES

After entering the user name and password in two input boxes, the user jumps to another activity and displays the contents of their input.

These two activity can then be easily redirected.



Second, the production process

1, first mainactivity login interface is the use of "Android" using the table layout, Android XML file and Java Interactive Production login interface (click Open link) layout, its layout file activity_ Main.xml as follows, the word has not changed:

<tablelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "match_parent" android:gravity= "center" > <!--First line--<tablerow Android        : layout_width= "match_parent" android:layout_height= "wrap_content" android:gravity= "Center_horizontal" >            <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:text= "@string/textview1" android:textsize= "24sp"/> <edittext android:id= "@+ Id/edittext1 "android:layout_width=" 200DP "android:layout_height=" Wrap_content "android:i        Nputtype= "text" android:textsize= "24sp"/> </TableRow> <!--second line--<tablerow Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "center_horizontal   "> <textview         Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "@str            Ing/textview2 "android:textsize=" 24sp "/> <edittext android:id=" @+id/edittext2 " Android:layout_width= "200DP" android:layout_height= "wrap_content" android:inputtype= "Textpasswor D "android:textsize=" 24sp "/> </TableRow> <!--third line--<tablerow Android:layo        Ut_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "Center_horizontal" > <button android:id= "@+id/button1" android:layout_width= "Match_parent" Android:layout_ height= "Wrap_content" android:text= "@string/button1"/> <button android:id= "@+id/button 2 "android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:text= "@string/button2"/>   </TableRow></TableLayout> 

Then, in Res/layout/string.xml, declare a new string for the new Activity button with a name of Activity2_button1, and the value is "off". Give the button in the new activity for a while.

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string    name= "app_name" > Login Interface </string >    <string name= "action_settings" >Settings</string>    <string name= "Textview1" > User name < /string>    <string name= "textview2" > Password </string>    <string name= "button1" > Login </string >    <string name= "Button2" > Cancel </string>    <string name= "Activity2_button1" > Close </string ></resources>

2, just the processing of the Java File Mainactivity.java button of the onclick processing method modified a bit.

Here, the bundle is used to store the variables to be in between multiple activity, which is equivalent to the map is composed of Key-value. Take out the values of the two editable boxes in the Mainactivity.java, put them in the bundle, and note that the items must first be converted to strings.

Intent indicates where the bundle is passed and what activity is going to be opened. Where Activity2.java is the activity we're going to create for a while.

Package Com.example.activitypass;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;import Android.widget.edittext;public class Mainactivity extends Activity {private Button Button1;private button button2;private EditText edittext1;private EditText edittext2; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button1 = (Button) Findviewbyid (r.id.button1), button2 = (button) Findviewbyid (r.id.button2); edittext1 = (EditText) Findviewbyid (R.ID.EDITTEXT1); edittext2 = (EditText) Findviewbyid (R.ID.EDITTEXT2); Button1.setonclicklistener (new Onclicklistener () {//For Button1 add click event @overridepublic void OnClick (View v) {String username = Edittext1.gettext (). ToString (); String password = Edittext2.gettext (). toString (); Intent Intent = new Intent (Mainactivity.this, Activity2. Class); Bundle bundle = new Bundle (), Bundle.putcharsequence ("username", username), bundle.putcharsequence ("Password", password); Intent.putextras (bundle); startactivity (intent);}); Button2.setonclicklistener (New Onclicklistener () {//For Button2 Add click event @overridepublic void OnClick (View v) {system.exit ( 0);//Exit Program}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
3, then we create a new activity, plainly, is in the com.example.xx (where xx is the project name), this package to create a new inherited android.app.activity Java file. Right-click com.example.xx to create a new class. This assumes that the new activity is named Activity2.java.

4, a Activity.java corresponds to an XML, so we also need in the Res/layout folder, Create a new activity2.xml, with a vertical linear layout in it, straight down, two ID TextView, a moment in Activity.java to put the bundle's data inside, and then the last button to close the activity. For a moment this activity2.xml the use of Setcontentview (r.layout.xxx) in Activity.java, which is used to associate this XML with Java.

<?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:o rientation= "vertical" >    <textview        android:id= "@+id/textview1" android:layout_width= "Match_        Parent "        android:layout_height=" wrap_content "        android:textsize=" 24sp "/>    <textview        android : id= "@+id/textview2"        android:layout_width= "match_parent"        android:layout_height= "Wrap_content        " Android:textsize= "24sp"/>    <button        android:id= "@+id/button1" android:layout_width= "Match_        Parent "        android:layout_height=" wrap_content "        android:text=" @string/activity2_button1 "/></ Linearlayout>
5. After that, the new activity must also be declared between the application nodes of the androidmanifest.xml, so that the newly created activity is finally completed. Otherwise, Android project is unable to find your new activity, you use StartActivity method once opened in Logcat error.

The Declaration method is very simple, the title of the activity is indicated by a label, and the implementation class of the activity is indicated by name.

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.activitypass "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SD K android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbacku" P= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/ Apptheme "> <activity android:name=" com.example.activitypass.MainActivity "Android:labe L= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action.m AIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter            > </activity> <activity android:name= "Com.example.activitypass.Activity2" Android:label= "@string/app_name"> </activity> </application></manifest> 
6, then, we go to activity2.java implementation of our new Activity2.java content, logic is very simple, modeled Mainactivity.java first to write things, OnCreate () method, those super inherited super class What, all written on. Then use Setcontentview (r.layout.activity2) to load the corresponding layout file. The contents of the bundle can be loaded into two TextView directly using SetText method.

Close activity uses the method of finish ().

package com.example.activitypass;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.textview;public class Activity2 extends Activity {private TextView Textview1;private TextView textview2;private Button button1; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity2); Intent Intent = Getintent (); Bundle bundle = Intent.getextras (); textView1 = (TextView) Findviewbyid (r.id.textview1); textView2 = (TextView) Findviewbyid (R.ID.TEXTVIEW2); button1 = (Button) Findviewbyid (R.id.button1); Textview1.settext ("Username:" + Bundle.getstring ("username")); Textview2.settext ("Password:" + bundle.getstring ("password")); Button1.setonclicklistener (New Onclicklistener () {//Button1 add click event @overridepublic void OnClick (View v) {finish ();//close This activity}});}} 
At this point the entire project development ended.

"Android" uses bundles to pass values between multiple activity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.