Android中一个Activity调用另一个Activity — Intent对象的使用 2010年04月12日 星期一 06:05 P.M. http://hi.baidu.com/soulingm/blog/item/f71c5ee495077d22b83820b2.html

 

实现步骤


第一步 :建立Android 工程:IntentDemo。


第二步 :编写Activity 的子类别:IntentDemo,其程序代码如下:


package com.a3gs.intent;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class IntentDemo extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super .onCreate(savedInstanceState);

        setContentView(R.layout.main );

       

        Button btn1 = (Button) findViewById(R.id.btn1 );

        btn1.setOnClickListener(new Button.OnClickListener(){

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub

              Intent intent = new Intent();

              intent.setClass(IntentDemo.this , Intent2.class );

              /* 调用一个新的 Activity */

              startActivity(intent);

              /* 关闭原本的 Activity */

              IntentDemo.this .finish ();

           }

        });

    }

}


第三步 :新建一个Activity 的子类别:Intent2,其程序代码如下:


package com.a3gs.intent;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;


public class Intent2 extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super .onCreate(savedInstanceState);

        setContentView(R.layout.mylayout );

       

        Button btn2 = (Button) findViewById(R.id.btn2 );

        btn2.setOnClickListener(new Button.OnClickListener(){

           @Override

           public void onClick(View v) {

              // TODO Auto-generated method stub

              Intent intent = new Intent();

              intent.setClass(Intent2.this , IntentDemo.class );

              startActivity(intent);

              Intent2.this .finish ();

           }

        });

    }

}


第四步 :修改res/layout/main.xml,其代码如下:


<?xml version="1.0" encoding="utf-8"?>

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >


Android中一个Activity调用另一个Activity — Intent对象的使用(2 2010年04月12日 星期一 06:05 P.M.

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/text1"

    />

    android:id="@+id/btn1"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/bt1"

    />

 

第四步 :修改res/layout/mylayout.xml,其代码如下:

 

<?xml version="1.0" encoding="utf-8"?>

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/text2"

    />

    android:id="@+id/btn2"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/bt2"

    />


Android中一个Activity调用另一个Activity — Intent对象的使用(3 2010年04月12日 星期一 06:06 P.M.

第五步 :修改AndroidManifest.xml,其内容如下:

 

<?xml version="1.0" encoding="utf-8"?>

      package="com.a3gs.intent"

      android:versionCode="1"

      android:versionName="1.0">

   

       

                  android:label="@string/app_name">

           < intent-filter>

               

               

           

       

      

   

 

扩展学习 :

 

当系统中新添加 Activity时,必须在 AndroidManifest.xml里定义一个新的 activity

否则系统将会因为找不到 Activity而发生编译错误。

另外,当程序中出现两个以上的 Activity时,系统要如何决定主程序是哪一支( entry point)呢?以本范例来说, AndroidManifest.xml Activity IntentDemo的定义如下:

android:label = "@string/app_name" >

       

    

其中有一行为 ,这就代表程序启动时,会先运行 IntentDemo这个 Activity,而非 Intent2。需注意的是,这个参数必须要被定义,如果 xml中没有一支 Activity有设置这个参数,则程序将不会被运行。

此外,在两支 Java程序中的最后一行都调用了 finish() 这个方法,它代表这个 Activity已运作完毕,当系统接收到这个命令时,即会关闭此 Activity,所以此时点击模拟器的返回( Back)键,并不会回到上一个 Activity的画面,如果要让模拟器的返回键有回上一页的效果,可以将此行程序注释掉。同理,当两个 Activity在切换时,并非真的只有两个 Activity在切换,而是在点击按钮时,再重新调用起一个新的 Activity

更多相关文章

  1. 用Eclipse开发和调试Android应用程序(一)
  2. Android中自定义Tab的实现
  3. Android利用drawable文件夹自定义控件背景、样式
  4. Android系统架构及特点
  5. 深入Android系统 Binder-3-原理
  6. 手机操作系统开源软件
  7. android系统开发小问题-启动过程中android字符没有显示出来
  8. -Android各版本系统源代码下载

随机推荐

  1. Android(安卓)RecyclerView瀑布流布局添
  2. Warning:Default Activity not found And
  3. 备战面试旺季:三年开发经验,离开了某创业公
  4. Android的进程管理 -奇怪且创新的进程哲
  5. android 字体样式
  6. Android社交系统----代码结构
  7. 基于Android小巫新闻客户端开发---显示新
  8. Android--------WebView+H5开发仿美团 预
  9. 从零开始--系统深入学习android(实践-让我
  10. Android学习札记50:在Android中自定义捕获