本文共 3735 字,大约阅读时间需要 12 分钟。
写了一个DEMO:短信选择联系人的数据传递
主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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View c){ //跳转至选择联系人Activity Intent intent = new Intent(this, ContactActivity.class);// startActivity(intent); //用这个api启动的Activity,在销毁时,系统会回调onActivityResult startActivityForResult(intent, 10); } public void click2(View v){ //跳转至选择快捷回复的Activity Intent intent = new Intent(this, CallbackActivity.class); startActivityForResult(intent, 20); } //如果有Activity在销毁时返回了数据,那么就会调用此方法来接收数据 //requestCode:用来区分数据来自于哪一个Activity //resultCode:用来区分返回的数据是什么类型的 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); String name = data.getStringExtra("name"); if(requestCode == 10){ EditText et = (EditText)findViewById(R.id.et); et.setText(name); } else if(requestCode == 20){ EditText et_content = (EditText)findViewById(R.id.et_content); et_content.setText(name); } } }联系人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 { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); ListView lv = (ListView) findViewById(R.id.lv); final String[] objects = new String[]{ "小志", "逼哥", "世界级XXX", "国服第一" }; lv.setAdapter(new ArrayAdapter快捷回复activity:(this, R.layout.item_listview, R.id.tv, objects)); //给listview设置条目的点击侦听 lv.setOnItemClickListener(new OnItemClickListener() { //当某个条目被点击时,此方法调用 @Override public void onItemClick(AdapterView parent, View view, int position, long id) { //Activity返回时传递数据,也是通过意图对象 Intent data = new Intent(); //把要传递的数据封装至意图对象中 data.putExtra("name", objects[position]); //当前Activity销毁时,data这个意图就会传递给启动当前Activity的那个Activity setResult(1, data); //销毁当前Activity finish(); } }); } @Override public void onBackPressed() { // TODO Auto-generated method stub super.onBackPressed(); }}
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 { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); ListView lv = (ListView) findViewById(R.id.lv); final String[] objects = new String[]{ "免谈,没戏,滚犊子", "媳妇我错了,求原谅", "老子才是一家之主" }; lv.setAdapter(new ArrayAdapter(this, R.layout.item_listview, R.id.tv, objects)); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { Intent data = new Intent(); data.putExtra("name", objects[position]); setResult(2, data); finish(); } }); }}