第一步:新建一个Android工程命名为MultiAcitivty目录结构如下图:
第二步:修改activity_main.xml布局文件代码如下:
- <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:orientation="vertical"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="startActivity"
- android:text="@string/start_activity" />
- </LinearLayout>
strings.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">MultiActivity</string>
- <string name="action_settings">Settings</string>
- <string name="hello_world">Hello world!</string>
- <string name="start_activity">打开一个新的Activity</string>
- <string name="other_activity">新的Activity</string>
- <string name="this_is_a_new_activity">这是一个新的Activity</string>
- <string name="go_back">返回</string>
- </resources>
第三步:新建一个OtherActivity.java。
- package cn.leigo.multiactivity;
- import android.app.Activity;
- import android.os.Bundle;
- public class OtherActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_other);
- }
- }
activity_other.xml:
- <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:orientation="vertical"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/this_is_a_new_activity" />
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/tv_age"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/go_back"
- android:onClick="goBack"/>
- </LinearLayout>
第四步:在AndroidManifest.xml文件中注册该Activity:
- <activity
- android:name="cn.leigo.multiactivity.OtherActivity"
- android:label="@string/other_activity" />
第五步:编写MainActivity类:
- package cn.leigo.multiactivity;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void startActivity(View v) {
- Intent intent = new Intent(this, OtherActivity.class);
- // Intent intent = new Intent();
- // intent.setClass(this, OtherActivity.class);//设置要激活的组建
- // Intent intent = new Intent();
- // intent.setClassName(this, "cn.leigo.multiactivity.OtherActivity");
- // Intent intent = new Intent();
- // intent.setClassName("cn.leigo.multiactivity",
- // "cn.leigo.multiactivity.OtherActivity");
- // Intent intent = new Intent();
- // intent.setComponent(new ComponentName(this, OtherActivity.class));
- //传值
- // intent.putExtra("name", "leigo");
- // intent.putExtra("age", 23);
- Bundle bundle = new Bundle();
- bundle.putString("name", "高磊");
- bundle.putInt("age", 23);
- intent.putExtras(bundle);
- // startActivity(intent);
- startActivityForResult(intent, 1);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- String blog = data.getStringExtra("blog");
- Toast.makeText(this, blog, Toast.LENGTH_SHORT).show();
- }
- }
第六步:重新编写OtherActivity类:
- package cn.leigo.multiactivity;
- 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 mNameText;
- private TextView mAgeText;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_other);
- Intent intent = getIntent(); // 用于激活它的意图对象
- //String name = intent.getStringExtra("name");
- //int age = intent.getIntExtra("age", 0);
- Bundle bundle = intent.getExtras();
- String name = bundle.getString("name");
- int age = bundle.getInt("age", 0);
- mNameText = (TextView)findViewById(R.id.tv_name);
- mAgeText = (TextView)findViewById(R.id.tv_age);
- mNameText.setText("姓名: " + name);
- mAgeText.setText("年龄:" + age);
- }
- public void goBack(View v) {
- Intent data = new Intent();
- data.putExtra("blog", "http://blog.csdn.net/u011272454");
- setResult(RESULT_OK, data); //设置返回的数据
- this.finish(); //关闭当前Activity
- }
- }