Activity A -----值-------> Activity B -----值-----> Activity A
package com.itheima.smsSender;import android.os.Bundle; import android.telephony.gsm.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.app.Activity; import android.content.Intent;/** * * 短信发送器: 三个界面 --- 三个Activity --- listview * */public class MainActivity extends Activity implements OnClickListener {private EditText et_number; private EditText et_content; private Button bt_select; private Button bt_sendSms; private Button bt_quickReply;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取控件 et_number = (EditText) findViewById(R.id.et_number); et_content = (EditText) findViewById(R.id.et_content); bt_select = (Button) findViewById(R.id.bt_select); bt_sendSms = (Button) findViewById(R.id.bt_sendSms); bt_quickReply = (Button) findViewById(R.id.bt_quickReply);// 添加点击事件 bt_select.setOnClickListener(this); bt_sendSms.setOnClickListener(this); bt_quickReply.setOnClickListener(this); }@SuppressWarnings("deprecation") @Override public void onClick(View v) { // TODO Auto-generated method stub int id = v.getId();switch (id) { case R.id.bt_select: Intent contact_intent = new Intent(getApplicationContext(), ContactActivity.class); startActivityForResult(contact_intent, 7); break; case R.id.bt_sendSms: SmsManager manager = SmsManager.getDefault(); String destinationAddress = et_number.getText().toString().trim(); String data = et_content.getText().toString().trim(); manager.sendTextMessage(destinationAddress , null, data , null, null); break; case R.id.bt_quickReply: Intent quick_intent = new Intent(getApplicationContext(), QuickReplyActivity.class); startActivityForResult(quick_intent, 8); break; }} @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case 7: String number = data.getStringExtra("number"); et_number.setText(number); break; case 8: String sms = data.getStringExtra("quickInfo"); et_content.setText(3); break; } } }
String number = data.getStringExtra("number");
package com.itheima.smsSender;import java.util.ArrayList;import com.itheima.smsSender.adapter.MyAdapter; import com.itheima.smsSender.bean.Person;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.ListView;public class ContactActivity extends Activity { ArrayList<Person> persons = new ArrayList<Person>(); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //加载布局文件 setContentView(R.layout.activity_contact); //初始化数据 --- 改进用内容提供者获取联系人 for(int i=0;i<30;i++){ Person person = new Person("person"+i,"1854678925"+i); persons.add(person); } //获取控件 ListView lv_contact = (ListView) findViewById(R.id.lv_contact); //添加适配器 lv_contact.setAdapter(new MyAdapter(persons,this)); //为适配器添加点击事件 lv_contact.setOnItemClickListener(new OnItemClickListener() {@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.putExtra("number", persons.get(position).phone); setResult(1, intent); finish(); } }); } }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (data == null) { //判断数据是否为空,就可以解决这个问题 return; } else { switch (requestCode) { case 7: String number = data.getStringExtra("number"); et_number.setText(number); break; case 8: String sms = data.getStringExtra("quickInfo"); et_content.setText(3); break; } } }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (resultCode) { case 1: String number = data.getStringExtra("number"); et_number.setText(number); break; case 3: String sms = data.getStringExtra("quickInfo"); et_content.setText(3); break; } } }