作者:刘昊昱

博客:http://blog.csdn.net/liuhaoyutz

一个Activity可以启动另外一个Activity,以实现比较复杂的功能,我们来看一个例子,其运行效果如下图所示:

主布局文件main.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="启动另外一个Activity示例:" /> <Button
android:id="@+id/button"
android:text="启动"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

主Activity内容如下所示:

package com.liuhaoyu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}

点击按钮时,通过Intent启动另外一个Activity,这里是SecondActivity,其内容如下:

package com.liuhaoyu;

import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}

SecondActivity的布局文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView"
android:text="这是被调用的Activity!"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />" </LinearLayout>

本程序中定义了两个Activity,需要在AndroidManifest.xml文件中声明SecondActivity:

        		<activity
android:icon="@drawable/ic_launcher"
android:name=".SecondActivity"
android:label="Activity"
android:theme="@android:style/Theme.Dialog"
>
</activity>

最新文章

  1. FormData、Blob、File、ArrayBuffer数据类型
  2. IIS does not list a website that matches the launch url
  3. C++中的构造函数,拷贝构造函数和赋值运算
  4. log4j的配置信息(转)
  5. Java中String字符串常量池总结
  6. 2016/1/9:深度剖析安卓Framebuffer设备驱动
  7. weblogic domain creation
  8. CSS设置表格TD宽度布局
  9. Notepad++ 中使用tail -f功能
  10. ROC,AUC,Precision,Recall,F1的介绍与计算
  11. HTML 常用小技巧 【标题图标】【锚点】【插入音乐,视频】【滚动效果】【嵌入网页】
  12. [Windows]Win10下VM虚拟机桥接模式无法上网的解决办法
  13. POJ 1730 Perfect Pth Powers(唯一分解定理)
  14. 借助 Filter 生成静态页面缓存问题
  15. spark 系列文章汇总
  16. (五)Redis集合Set操作
  17. Promise超时重新请求
  18. jenkins-APP打包页面展示二维码【转】
  19. HOOK技术演示
  20. WPF 界面提示加载出错

热门文章

  1. Spring中给Bean注入集合
  2. poj 2046 Gap(bfs+hash)
  3. (转)iOS7界面设计规范(8) - UI基础 - 术语和措辞
  4. Class的生命周期
  5. tomcat下配置https环境
  6. VMware vSphere 5.5的12个更新亮点(2)
  7. Android实现左右滑动效果
  8. 22个CSS黑魔法
  9. UITableView 总结
  10. iOS的触摸事件