第一步:新建一个继承Activity的类,如:NewActivity
1
2
3
4
5
6 |
public class NewActivity extends
Activity { 2
@Override protected void onCreate(Bundle savedInstanceState) { 3
super .onCreate(savedInstanceState); 4
//这里可以使用setContentView(R.layout.xxx)显示某个视图. 5
} 6 } |
第二步:需要在功能清单AndroidManifest.xml文件中添加进上面Activity配置代码:
1
2
3
4
5
6
7
8
9
10 |
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" 2
package = "com.gaolei.activity" 3
android:versionCode= "1" 4
android:versionName= "1.0" > 5
<application android:icon= "@drawable/icon"
android:label= "@string/app_name" > 6
.. 7
<activity android:name= ".NewActivity"
android:label= "新activity的页面标题" /> 8
</application> 9 10 </manifest> |
1
2
3
4
5
6
7
8
9
10
11 |
public class MainActivity extends
Activity { 2
@Override protected void onCreate(Bundle savedInstanceState) { 3
. 4
Button button =(Button) this .findViewById(R.id.button); 5
button.setOnClickListener( new
View.OnClickListener(){ //点击该按钮会打开一个新的Activity 6
public void onClick(View v) { 7
//新建一个显式意图,第一个参数为当前Activity类对象,第二个参数为你要打开的Activity类 8
startActivity( new
Intent(MainActivity. this , NewActivity. class )); 9
}}); 10
} 11
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 |
public class MainActivity extends
Activity { 2
@Override protected void onCreate(Bundle savedInstanceState) { 3
. 4
button.setOnClickListener( new
View.OnClickListener(){ //点击该按钮会打开一个新的Activity 5
public void onClick(View v) { 6
Intent intent = new
Intent(MainActivity. this , NewActivity. class ) 7
Bundle bundle = new
Bundle(); //该类用作携带数据 8
bundle.putString( "name" , "gaolei" ); 9
bundle.putInt( "age" , 23 ); 10
intent.putExtras(bundle); //附带上额外的数据 11
startActivity(intent); 12
} }); } 13
} |
在新的Activity中接收前面Activity传递过来的参数:
1
2
3
4
5
6
7
8 |
1 public class NewActivity extends
Activity { 2
@Override protected void onCreate(Bundle savedInstanceState) { 3
.. 4
Bundle bundle = this .getIntent().getExtras(); 5
String name = bundle.getString( "name" ); 6
int age = bundle.getInt( "age" ); 7
} 8 } |
Bundle类的作用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
public final class Bundle implements
Parcelable, Cloneable { 2 3
Map<String, Object> mMap; 4
public Bundle() { 5
mMap = new
HashMap<String, Object>(); 6 7
} 8
public void putString(String key, String value) { 9
mMap.put(key, value); 10
} 11
public String getString(String key) { 12
Object o = mMap.get(key); 13
return (String) o; 14
.. //类型转换失败后会返回null,这里省略了类型转换失败后的处理代码 15
} 16
} |
Bundle类用作携带数据,它类似于Map,用于存放key-value名值对形式的值。相对于Map,它提供了各种常用类型的putXxx()/getXxx()方法,如:putString()/getString()和putInt()/getInt(),putXxx()用于往Bundle对象放入数据,getXxx()方法用于从Bundle对象里获取数据。Bundle的内部实际上是使用了HashMap<String, Object>类型的变量来存放putXxx()方法放入的值:
1
2
3
4 |
Intent intent = new
Intent(); 2 Bundle bundle = new
Bundle(); //该类用作携带数据 3 bundle.putString( "name" , "gaolei" ); 4 intent.putExtras(bundle); //为意图追加额外的数据,意图原来已经具有的数据不会丢失,但key同名的数据会被替换 |
第二种写法:这种写法的作用等价于上面的写法,只不过这种写法是把数据一个个地添加进Intent,这种写法使用起来比较方便,而且只需要编写少量的代码。
1 |
Intent提供了各种常用类型重载后的putExtra()方法,如: putExtra(String name, String value)、 putExtra(String name, long
value),在putExtra()方法内部会判断当前Intent对象内部是否已经存在一个Bundle对象,如果不存在就会新建Bundle对象,以后调用putExtra()方法传入的值都会存放于该Bundle对象,下面是Intent的putExtra(String name, String value)方法代码片断: |
1
2
3
4
5
6
7
8
9 |
public class Intent implements
Parcelable { 2 private Bundle mExtras; 3 public Intent putExtra(String name, String value) { 4
if (mExtras == null ) { 5
mExtras = new
Bundle(); 6
} 7
mExtras.putString(name, value); 8
return this ; 9 } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
public class MainActivity extends
Activity { 2
@Override protected void onCreate(Bundle savedInstanceState) { 3
. 4
Button button =(Button) this .findViewById(R.id.button); 5
button.setOnClickListener( new
View.OnClickListener(){ //点击该按钮会打开一个新的Activity 6
public void onClick(View v) { 7
//第二个参数为请求码,可以根据业务需求自己编号 8
startActivityForResult ( new
Intent(MainActivity. this , NewActivity. class ), 1 ); 9
}}); 10
} 11
//第一个参数为请求码,即调用startActivityForResult()传递过去的值 12
//第二个参数为结果码,结果码用于标识返回数据来自哪个新Activity 13
@Override protected void onActivityResult( int
requestCode, int
resultCode, Intent data) { 14
String result = data.getExtras().getString(“result”)); //得到新Activity 关闭后返回的数据 15
} 16
} |
1
2
3
4
5
6
7
8
9
10
11
12 |
public class NewActivity extends
Activity { 2
@Override protected void onCreate(Bundle savedInstanceState) { 3 4
button.setOnClickListener( new
View.OnClickListener(){ 5
public void onClick(View v) { 6
Intent intent = new
Intent(); //数据是使用Intent返回 7
intent.putExtra(“result”, “返回数据”); //把返回数据存入Intent 8
NewActivity. this .setResult(RESULT_OK, intent); //设置返回数据 9
NewActivity. this .finish(); //关闭Activity 10
}}); 11
} 12
} |
setResult()方法的第一个参数值可以根据业务需要自己定义,上面代码中使用到的RESULT_OK是系统Activity类定义的一个常量,值为-1,代码片断如下:
1
2
3
4
5 |
public class android.app.Activity extends
{ 2
public static final int RESULT_CANCELED = 0 ; 3
public static final int RESULT_OK = - 1 ; 4
public static final int RESULT_FIRST_USER = 1 ; 5 } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
@Override
public void onCreate(Bundle savedInstanceState) { 2
. 3
button1.setOnClickListener( new
View.OnClickListener(){ 4
public void onClick(View v) { 5
startActivityForResult ( new
Intent(MainActivity. this , NewActivity. class ), 1 ); 6
}}); 7
button2.setOnClickListener( new
View.OnClickListener(){ 8
public void onClick(View v) { 9
startActivityForResult ( new
Intent(MainActivity. this , NewActivity. class ), 2 ); 10
}}); 11
@Override protected void onActivityResult( int
requestCode, int
resultCode, Intent data) { 12
switch (requestCode){ 13
case 1 : 14
//来自按钮1的请求,作相应业务处理 15
case 2 : 16
//来自按钮2的请求,作相应业务处理 17
} 18
} 19
} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 |
public class ResultActivity extends
Activity { 2
.. 3
ResultActivity. this .setResult( 1 , intent); 4
ResultActivity. this .finish(); 5
} 6
public class NewActivity extends
Activity { 7 8
NewActivity. this .setResult( 2 , intent); 9
NewActivity. this .finish(); 10 } 11
public class MainActivity extends
Activity { // 在该Activity会打开ResultActivity和NewActivity 12
@Override protected void onActivityResult( int
requestCode, int
resultCode, Intent data) { 13
switch (resultCode){ 14
case 1 : 15
// ResultActivity的返回数据 16
case 2 : 17
// NewActivity的返回数据 18
} 19
} 20
} 21 |
为应用添加多个Activity与参数传递,布布扣,bubuko.com
原文:http://www.cnblogs.com/lomomiao/p/3597449.html