Android基础篇之Activity跳转

在AndroidManifest.xml中有如下定义:

1
2
3
4
5
6
< activity  android:name = ".MainActivity"  android:icon = "@drawable/app_icon" >
     < intent-filter >
         < action  android:name = "android.intent.action.MAIN"  />
         < category  android:name = "android.intent.category.LAUNCHER"  />
     </ intent-filter >
</ activity >

1、<activity>节点中可以使用<intent-filter>指定各种各样的过滤器。这样是为了其他应用组件可以通过这个过滤器激活它。

2、<action>节点指定了这个应用程序的入口点,就是当我们打开应用程序的时候第一个执行的Activity.相当于java程序的中的main函数。

3、<categroy>节点指定了这个Activity在系统的应用程序启动器中列出。


在一个Activity中启动另一个Activity(显式启动)

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
< RelativeLayout  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:paddingLeft = "@dimen/activity_horizontal_margin"
     android:paddingRight = "@dimen/activity_horizontal_margin"
     android:paddingTop = "@dimen/activity_vertical_margin"
     android:paddingBottom = "@dimen/activity_vertical_margin"
     tools:context = ".MainActivity" >
            
     < TextView
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:text = "@string/one_activity"  />
            
     < Button
         android:layout_width = "wrap_content"
         android:layout_marginTop = "30dip"
         android:layout_height = "wrap_content"
         android:id = "@+id/btn_main_start"
         android:text = "@string/start"
             />
            
</ RelativeLayout >


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package  com.android_learn.activity;
             
import  android.content.Intent;
import  android.os.Bundle;
import  android.app.Activity;
import  android.view.Menu;
import  android.view.View;
import  android.widget.Button;
             
             
public  class  MainActivity  extends  Activity {
             
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         Button btn = (Button)findViewById(R.id.btn_main_start);
         btn.setOnClickListener( new  View.OnClickListener() {
             @Override
             public  void  onClick(View view) {
           
                   //使用startActivity(当前activity,目标activity)跳转
                 startActivity( new  Intent(MainActivity. this ,SubActivity. class ));
             }
         });
     }
             
             
     @Override
     public  boolean  onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return  true ;
     }
                 
}

activity_sub.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<? xml  version = "1.0"  encoding = "utf-8" ?>
            
< LinearLayout  xmlns:android = "http://schemas.android.com/apk/res/android"
               android:orientation = "vertical"
               android:layout_width = "match_parent"
               android:layout_height = "match_parent" >
            
     < TextView
             android:layout_width = "match_parent"
             android:layout_height = "match_parent"
             android:text = "@string/two_activity"
     >
            
     </ TextView >
            
</ LinearLayout >


SubActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package  com.android_learn.activity;
             
import  android.app.Activity;
import  android.os.Bundle;
             
/**
  * Created by gqs on 5/27/13.
  */
public  class  SubActivity  extends  Activity {
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_sub);
     }
}

通过上面的例子,就是实现了两个Activity之间的跳转,Activity的隐式跳转:

在AndroidManifest.xml中的目标<activity>节点自定一个action

1
2
3
4
5
6
7
8
< activity
                 android:name = "com.android_learn.activity.SubActivity"
                 android:label = "@string/app_name"  >
             < intent-filter >
                 < action  android:name = "toactivity" ></ action >//自定义的action
                 < category  android:name = "android.intent.category.DEFAULT"  />
             </ intent-filter >
         </ activity >

在要执行跳转的Activity中必须要指定Intent.setAction("自定义的action值");

1
2
3
4
5
6
final  static  String ACTION =  "toactivity" ;
public  void  onClick(View view) {
               // startActivity(new Intent(MainActivity.this,SubActivity.class));
                Intent intent =  new  Intent();
                startActivity(intent.setAction(ACTION));
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值