从一个Activity打开另外一个Activity-LMLPHP

从一个Activity打开另外一个Activity-LMLPHP

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);
} public void openActivity(View v){
//打开新的Activity
Intent intent = new Intent(this, OtherActivity.class);//激活组件,显示意图:明确指定了组件名称的意图叫显示意图
//关于指定组件名称,还有很多写法
//1> intent.setClass(this, OtherActivity.class);//指定要激活的组件名称
//2> intent.setClassName(this, "cn.itcast.activitys.OtherActivity");//还可以激活别的应用的Activity
//3> intent.setComponent(new ComponentName(this, OtherActivity.class));
// intent.setClassName(this, "cn.itcast.activitys.OtherActivity");
/*
intent.putExtra("company", "传智播客");
intent.putExtra("age", 5);
*/
Bundle bundle = new Bundle();
bundle.putString("company", "CSDN");
bundle.putInt("age", 11);
intent.putExtras(bundle); //startActivity(intent);
startActivityForResult(intent, 100);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String result = data.getStringExtra("result");
Toast.makeText(getApplicationContext(),result, 1).show(); super.onActivityResult(requestCode, resultCode, data);
} }
public class OtherActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other); TextView textView = (TextView) this.findViewById(R.id.textView); 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"); textView.setText("公司名称:"+ company+ ",年限:"+ age);
} public void closeActivity(View v){
Intent intent = new Intent();
intent.putExtra("result", "老方与丽丽的故事,后面省略2000字");
setResult(20, intent);//设置返回数据
finish();//关闭Activity(关闭窗口)
}
}
05-11 15:47