代码结构图
manifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="cn.com.chenzheng_java"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
-
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".ChangeActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- <activity android:name=".ChangeActivity2" android:label="第二个activity正在展示"></activity>
- </application>
- </manifest>
两个activity的代码
- package cn.com.chenzheng_java;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class ChangeActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- Button button = (Button)findViewById(R.id.button);
- button.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
- Intent intent = new Intent();
-
- intent.setClass(ChangeActivity.this, ChangeActivity2.class);
-
- Bundle bundle = new Bundle();
- bundle.putString("name", "chenzheng_java");
- bundle.putInt("age", 23);
- intent.putExtras(bundle);
-
- startActivity(intent);
-
- ChangeActivity.this.finish();
-
- }
- });
-
- }
- }
- package cn.com.chenzheng_java;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
-
- public class ChangeActivity2 extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout2);
-
- TextView textView = (TextView)findViewById(R.id.textView);
-
- Intent intent = this.getIntent();
- Bundle bundle = intent.getExtras();
- String result = "";
- result+="姓名: "+bundle.getString("name");
- result+=" 年龄:"+bundle.getInt("age");
- textView.setText(result);
-
-
-
- }
- }
---------------------------------------------
这里主要研究了下,怎么从一个activity跳转到另一个activity。我们可以看到,activity之间的跳转以及数据交互都离不开一个类,那就是Intent。