Activity 返回数据


AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xiesir.example26returndata"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ContactActivity"></activity> <activity android:name=".HandleExceptionActivity"></activity> <activity android:name=".SmsActivity"></activity> </application> </manifest>
item_listview.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="26sp" /> </LinearLayout>
activity_contact.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView> </LinearLayout>
activity_handleexception.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="老婆" android:textSize="26sp" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="真正的道歉是无武装地舍弃自己。老婆,我以后一切都听你的了。" android:textSize="26sp" /> </LinearLayout>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.xiesir.example26returndata.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/et_name" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="联系人" android:onClick="click" /> </LinearLayout> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:ems="10" > <requestFocus /> </EditText> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="快捷回复" android:onClick="click2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="道歉" android:onClick="click3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" /> </LinearLayout> </LinearLayout>
ContactActivity.java:
package com.xiesir.example26returndata; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; /** * Created by xiegly on 2016/6/5. */ public class ContactActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); final String[] names = new String[] {"孙悟空", "朱八戒", "撒和尚"}; // 显示联系人列表 ListView lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, names)); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { // position:点击某个条目时,会通过position告诉程序员点击的是哪一个条目 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent data = new Intent(); // 把要传递的数据封装至intent中 data.putExtra("name", names[position]); // 当此Activity销毁时,返回至上一个Activity时,会把这个intent对象传递给上一个Activity setResult(10, data); // 销毁当前Activity finish(); } }); } }
SmsActivity.java:
package com.xiesir.example26returndata; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; /** * Created by xiegly on 2016/6/5. */ public class SmsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); final String[] smss = new String[]{ "我正在上课,一会回你", "我将春天付给了你,将冬天留给我自己。我将你的背影留给我自己,却将自己给了你。", "我所认为最深沉的爱,莫过于分开以后,我将自己活成了你的样子。" }; // 显示联系人列表 ListView lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv, smss)); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent data = new Intent(); data.putExtra("sms", smss[position]); setResult(20, data); finish(); } }); } }
HandleExceptionActivity.java:
package com.xiesir.example26returndata; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; /** * Created by xiegly on 2016/6/5. */ public class HandleExceptionActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_handleexception); final TextView tv_name = (TextView) findViewById(R.id.tv_name); final TextView tv_content = (TextView) findViewById(R.id.tv_content); tv_name.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent data = new Intent(); data.putExtra("name", tv_name.getText()); setResult(100, data); finish(); } }); tv_content.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent data = new Intent(); data.putExtra("sms", tv_content.getText()); setResult(200, data); finish(); } }); } }
MainActivity.java:
package com.xiesir.example26returndata; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { public static final int CONTACT = 10; public static final int SMS = 20; public static final int HANDLE_EXCEPTION = 30; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v){ Intent intent = new Intent(this, ContactActivity.class); //启动一个Activity去获取数据(结果) startActivityForResult(intent, CONTACT); } public void click2(View v){ Intent intent = new Intent(this, SmsActivity.class); startActivityForResult(intent, SMS); } public void click3(View v){ Intent intent = new Intent(this, HandleExceptionActivity.class); startActivityForResult(intent, HANDLE_EXCEPTION); } // 只有通过startActivityForResult启动的Activity销毁时,才会回调这个方法,方法中传入的intent就封装了返回的数据 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //通过请求码来判断data来自哪一个Activity if(requestCode == CONTACT){ String name = data.getStringExtra("name"); EditText et_name = (EditText) findViewById(R.id.et_name); et_name.setText(name); } else if(requestCode == SMS){ String sms = data.getStringExtra("sms"); EditText et_content = (EditText) findViewById(R.id.et_content); et_content.setText(sms); } //先通过请求码判断data来自哪个Activity else if(requestCode == HANDLE_EXCEPTION){ //通过结果码判断数据data中的数据是什么类型 if(resultCode == 100){ String name = data.getStringExtra("name"); EditText et_name = (EditText) findViewById(R.id.et_name); et_name.setText(name); } else if(resultCode == 200){ String sms = data.getStringExtra("sms"); EditText et_content = (EditText) findViewById(R.id.et_content); et_content.setText(sms); } } } }
参考:
浙公网安备 33010602011771号