实例教程十二:为应用添加多个Activity与参数传递
原文链接:http://www.eoeandroid.com/thread-204945-1-1.html
同时不错的帖子:
Andorid页面拨打电话
http://www.eoeandroid.com/thread-211456-1-1.html
仿QQ的源码,客户端和服务端都有的啦
http://www.eoeandroid.com/thread-212343-1-1.html
手机铃声软件-android源代码
http://www.eoeandroid.com/thread-211143-1-1.html
-------------------帖子正文---------------
MainActivity效果图
OtherActivity.xml效果图
结果图
package cn.itcast.activitys; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } //Intent --激活组件(Activity),附带数据 public void openActivity(View v){ Intent intent = new Intent(); //只能激活本应用里的Activity //传数据 //1. //intent.putExtra("company", "iflytek"); //intent.putExtra("age", 5); //2.批量添加传递数据 Bundle bundle = new Bundle(); bundle.putString("company", "CSDN"); bundle.putInt("age", 10); intent.putExtras(bundle); //第一种方法 intent.setClass(this, OtherActivity.class); //设置要激活的组件 //第二种方法 //intent.setClassName(this, "cn.itcast.activitys.OtherActivity"); //第三种方法 //intent.setComponent(new ComponentName(this, OtherActivity.class)); //第四种方法 -- 显式Intent //Intent intent = new Intent(this, OtherActivity.class); //跨应用激活Activity //第5种方法-- //intent.setClassName("别的应用的包名", "别的应用的Activity"); //例-- intent.setClassName("cn.itcast.activitys", "cn.itcast.activitys.OtherActivity"); //startActivity(intent); //在OtherActivity关闭后,把处理的数据返回给MainActivity //那必须在MainActivity打开时,选择startActivityForResult() startActivityForResult(intent, 200); } //用于得到OtherActivity关闭后返回的结果数据 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { String result = data.getStringExtra("result"); Toast.makeText(this, result, 1).show(); } }
package cn.itcast.activitys; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class OtherActivity extends Activity { private TextView txtShow; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); //可用intent.setAction()来区分 //第一种,适用传递方法第1种和第2种 Intent intent = getIntent(); //用于激活它的意图对象 //String company = intent.getStringExtra("company"); //int age = intent.getIntExtra("age", 0); Bundle bundle = intent.getExtras(); String company = bundle.getString("company"); int age = bundle.getInt("age"); //接收数据第2种方法 txtShow = (TextView)this.findViewById(R.id.txtShow); txtShow.setText("公司名:" + company + ",成立" + age + "年"); //在OtherActivity关闭后,把处理的数据返回给MainActivity //那必须在MainActivity打开时,选择startActivityForResult() } public void closeActivity(View v){ Intent data = new Intent(); data.putExtra("result", txtShow.getText()); //第一个参数--激活码-供MainActivity得知结果码 //来自于OtherActivity的哪一个事件或哪一项数据 setResult(200, data); //设置返回数据 this.finish(); //关闭当前Activity } }