比如:你写了两个activity:AAct、BAct。要想实现两个窗体的跳转,首先必须在AndroidManifest.xml中进行声明:
声明的内容有:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AAct"
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="BAct"></activity>
</application>
</manifest>
传递参数方法一:A的onCreate()中:
Intent intent=new Intent();
String one=myEditText1.getText().toString();
String two=myEditText2.getText().toString();
intent.putExtra("num1", one);
intent.putExtra("num2", two);
intent.setClass(ActivityAct.this,result.class);
ActivityAct.this.startActivity(intent);
接受参数:B的onCreate()中:
Intent intent=getIntent();
int aa=Integer.parseInt(intent.getStringExtra("num1"));
int bb=Integer.parseInt(intent.getStringExtra("num2"));
传递参数方法二:A的onCreate()中:
Intent intent=new Intent();
intent.setClass(act1.this,act2.class);
Bundle bundle=new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
intent.putExtras(bundle);
startActivity(intent);
接受参数:B的onCreate()中:
//取得intent中的Bundle对象
Bundle bundle=this.getIntent().getExtras();
String sex=bundle.getString("sex");
double height=bundle.getDouble("height");
声明的内容有:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AAct"
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="BAct"></activity>
</application>
</manifest>
传递参数方法一:A的onCreate()中:
Intent intent=new Intent();
String one=myEditText1.getText().toString();
String two=myEditText2.getText().toString();
intent.putExtra("num1", one);
intent.putExtra("num2", two);
intent.setClass(ActivityAct.this,result.class);
ActivityAct.this.startActivity(intent);
接受参数:B的onCreate()中:
Intent intent=getIntent();
int aa=Integer.parseInt(intent.getStringExtra("num1"));
int bb=Integer.parseInt(intent.getStringExtra("num2"));
传递参数方法二:A的onCreate()中:
Intent intent=new Intent();
intent.setClass(act1.this,act2.class);
Bundle bundle=new Bundle();
bundle.putDouble("height",height);
bundle.putString("sex",sex);
intent.putExtras(bundle);
startActivity(intent);
接受参数:B的onCreate()中:
//取得intent中的Bundle对象
Bundle bundle=this.getIntent().getExtras();
String sex=bundle.getString("sex");
double height=bundle.getDouble("height");