Android interface jump and data exchange

Source: Internet
Author: User

Ching

Original address: http://blog.csdn.net/qingdujun/article/details/40015757


This article demonstrates: Android interface jump and data exchange, through a small demo show the whole process.

The effect is as follows:

1) Mainactivity.java

Package Doogle.xian.bundletest;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;public Class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate        (savedinstancestate);        Setcontentview (R.layout.activity_main);        Button btn = (button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {EditText name = (EditText) Findviewbyid (R.id.name); EditText age = (EditText) Findviewbyid (r.id.age); CData send = new CData (Name.gettext (). ToString (), Integer.parseint (Age.gettext (). ToString ())); Create a Bundle object bundle data = new Bundle ();d ata.putserializable ("MyData", send);//Create a Intent object Intent intent = new Intent ( Mainactivity.this,resultactivity.class); IntenT.putextras (data);//start Intent corresponding activitystartactivity (intent);}}); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (R.menu.main, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item); }}

2) Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Doogle.xian.bundletest.MainActivity" > <textview android:id= "@+id/tv" android:layout_width        = "Wrap_content" android:layout_height= "wrap_content" android:text= "passed to Activity2:"/> <edittext Android:id= "@+id/name" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android oid:layout_below= "@id/tv" android:hint= "name"/> <edittext android:id= "@+id/age" Android:lay Out_width= "Fill_parent" android:layout_height= "Wrap_conTent "android:layout_below=" @id/name "android:hint=" Age "/> <button android:id=" @+id/btn "         Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_below= "@id/age" android:text= "Open Activity2"/></relativelayout>

3) Resultactivity.java

Package Doogle.xian.bundletest;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.widget.textview;public class Resultactivity extends Activity {@Override    protected void OnCreate ( Bundle savedinstancestate) {        super.oncreate (savedinstancestate);        Setcontentview (R.layout.result);        TextView TV1 = (TextView) Findviewbyid (R.ID.TV1);        TextView TV2 = (TextView) Findviewbyid (R.ID.TV2);        Obtain the Intent        Intent Intent = getintent () that initiates the result        ; Remove the data from the bundle packet it carries directly through intent        CData accept = (CDATA) Intent.getserializableextra ("MyData");        Tv1.settext ("name" +accept.getname ());        Tv2.settext ("Age" +accept.getage ());}    }
4) Result.xml

<?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/tv"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "received data:"/> <textview android:id=        "@+ ID/TV1 "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text=" "/>    <textview        android:id=" @+id/tv2 "        android:layout_width=" Wrap_content "        android: layout_height= "Wrap_content"        android:text= ""/></linearlayout>
5) Cdata.java

Package Doogle.xian.bundletest;import Java.io.serializable;public class CData implements Serializable{private static Final long serialversionuid = 1L; String Name;int age;public CData (string n, int a) {name = N;age = A;} Public String GetName () {return name;} public int getage () {return age;}}
6) Androidmainfest.xml

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Doogle.xian.bundletest "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "android:targetsdkversion="/> <application android:allowbackup = "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/A Pptheme "> <activity android:name=". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER            "/> </intent-filter> </activity> <!--configuring activity--<activity Android:name= ". Resultactivity "android:label=" Activity2 "> </activitY> </application></manifest> 

Original address: http://blog.csdn.net/qingdujun/article/details/40015757


Android interface jump and data exchange

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.