Android--activity Get return value

Source: Internet
Author: User

Wrote a demo: SMS select the data transfer of the contact person

Main activity:

Import Android.os.bundle;import android.app.activity;import Android.content.intent;import Android.view.Menu;import Android.view.view;import Android.widget.edittext;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} public void Click (View c) {//jump to select contact Activityintent intent = new Intent (this, contactactivity.class);//startactivity ( intent);//activity initiated with this API, when destroyed, the system will callback Onactivityresultstartactivityforresult (intent, 10);} public void Click2 (View v) {//jump to select Activityintent Intent for quick reply = new Intent (this, callbackactivity.class); Startactivityforresult (Intent, 20);} If an activity returns data when it is destroyed, this method is called to receive data//requestcode: to distinguish which activity//resultcode the data comes from: Used to differentiate what type of data is returned @overrideprotected void onactivityresult (int requestcode, int resultcode, Intent data) {//TODO Auto-generated method Stubsuper.onactivityresult (Requestcode, ResultCode, data); String name = Data.getstringextra ("Name "), if (Requestcode = =) {EditText et = (EditText) Findviewbyid (r.id.et); Et.settext (name);} else if (Requestcode = =) {EditText et_content = (EditText) Findviewbyid (r.id.et_content); Et_content.settext (name);}}}
Contact activity:

Import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.view.View;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.listview;public class Contactactivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_contact); ListView LV = (ListView) Findviewbyid (r.id.lv); final string[] objects = new string[]{"Xiao Zhi", "Brother forced", "world class XXX", "Country Service First"}; Lv.setadapter (New arrayadapter<string> (this, R.layout.item_listview, R.id.tv, objects));// Click to listen Lv.setonitemclicklistener (new Onitemclicklistener () {////When an entry is clicked, this method calls @overridepublic void for the ListView setting entry Onitemclick (adapterview<?> parent, View view,int position, long ID) {//activity passes data when returned, also by intent object intent data = new in Tent ();//encapsulates the data to be passed into the intent object Data.putextra ("name", Objects[position]);//The current activity is destroyed,The intent of data is passed to the Activitysetresult (1, data) that initiates the current activity;//destroys the current activityfinish ();});} @Overridepublic void onbackpressed () {//TODO auto-generated method Stubsuper.onbackpressed ();}}

Quick reply activity:

Import Android.app.activity;import android.content.intent;import Android.os.bundle;import Android.view.View;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.arrayadapter;import Android.widget.listview;public class Callbackactivity extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_contact); ListView LV = (ListView) Findviewbyid (r.id.lv); final string[] objects = new string[]{"No, no, no, roll Duzi", "daughter-in-law I was wrong, beg forgiveness", "Lao Tzu is the head of the household" };lv.setadapter (New arrayadapter<string> (this, R.layout.item_listview, R.id.tv, objects)); Lv.setonitemclicklistener (New Onitemclicklistener () {@Overridepublic void Onitemclick (adapterview<?> parent, View view,int position, long ID) {Intent data = new Intent ();d Ata.putextra ("name", Objects[position]); Setresult (2, data); Finish ();}});}}

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "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= ".        Mainactivity "android:orientation=" vertical "> <linearlayout android:layout_width=" match_parent " android:layout_height= "wrap_content" android:orientation= "Horizontal" > <edittext Android Oid:id= "@+id/et" android:layout_weight= "1" android:layout_width= "0DP" android:layout_height= "Wrap_con Tent "android:hint=" Please enter the contact "/> <button android:layout_weight=" 0 "Android:layout_widt H= "Wrap_content" android:layout_height= "Wrap_coNtent "android:text=" + "android:onclick=" click "/> </LinearLayout> <edittext Android:id= "@+id/et_content" android:layout_weight= "1" android:layout_width= "Match_parent" android:l ayout_height= "0DP" android:hint= "Please enter SMS Content" android:gravity= "top"/><button android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content" android:text= "quick reply" android:onclick= "Click2"/>< Button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Send SMS"/>< /linearlayout>

<?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" >        <listview         android:id= "@+id/lv"        android:layout_width= "Match_parent"        android:layout_height= "Match_parent"        ></ListView></LinearLayout>

<?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:textsize= "20sp"        /></linearlayout>



Android--activity Get return value

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.