Android(java)学习笔记131:Intent启动别的Activity
1. Intent启动Activity案例
(1)首先是main.xml和other.xml文件如下:
main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is MainActivity!"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OpenOtherActivity!"
android:id="@+id/btnOpen"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="HideActivity"
android:id="@+id/btnHideActivity"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ExitActivity"
android:id="@+id/btnExitActivity"
/>
</LinearLayout>
接着是other.xml文件:
<?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/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" /> </LinearLayout>
(2)MainActivity.java 和 OtherActivity.java文件:
MainActivity.java:
package com.openother; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener {
//声明按钮
private Button btnOpen, btnHideActivity, btnExitActivity; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//实例按钮
btnOpen = (Button) findViewById(R.id.btnOpen);
btnHideActivity = (Button) findViewById(R.id.btnHideActivity);
btnExitActivity = (Button) findViewById(R.id.btnExitActivity);
//给每个按钮添加监听
btnOpen.setOnClickListener(this);
btnHideActivity.setOnClickListener(this);
btnExitActivity.setOnClickListener(this);
} public void onClick(View v) {
if (v == btnOpen) {
//创建一个意图,并且设置需打开的Activity
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
//发送数据
intent.putExtra("Main", "我是发送的数据~娃哈哈");
//启动另外一个Activity
this.startActivity(intent);
} else if (v == btnHideActivity) {
this.finish();//退出Activity
}else if (v == btnExitActivity) {
System.exit(0);//退出程序
}
}}
其次是OtherActivity.java:
/**
*
*/
package com.openother; import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView; /**
* @author Himi
*
*/
public class OtherActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);//设置当前的OtherActivity的视图内容绑定R.layout.other的布局文件
tv = (TextView)findViewById(R.id.textView1);
tv.setText("爱一个人好难"); AssetManager mgr = getAssets();//AssetManager---管理应用程序的资产文件,在项目工程的assets文件夹下:
Typeface tf = Typeface.createFromAsset(mgr, "fonts/DroidSansFallback.ttf");
tv.setTypeface(tf);
}
}
2. 总结:
(1)在开发软件的过程中,两个activity之间的通讯可以通过bundle类来实现,它类似于Map,用于存放key-value名值对形式的值。遇到过这样一种情况,就是没有create bundle,但是当你使用intent.putExtra之后,在另一个被call的activity中,会有bundle被传递过去,
原因就是因为intent.putExtra时,系统会检测有没有bundle,如果没有,则新建一个。所以下面这两个语句的等效的:
Intent intent = new Intent(this,xxx.class);
intent.putExtra("test", true); startActivity(intent);
和:
Intent intent = new Intent(this,xxx.class);
Bundle bundle = new Bundle();
bundle.putBoolean("test", true);
intent.putExtras(bundle);
startActivity(intent);
这里的原因是我们可以追踪到putExtras()方法的底层如下:
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
Android(java)学习笔记131:Intent启动别的Activity的更多相关文章
- Android开发学习笔记:Intent的简介以及属性的详解【转】
一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...
- 【转载】Android开发学习笔记:Intent的简介以及属性的详解
http://liangruijun.blog.51cto.com/3061169/634411/ 一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent ...
- Android:日常学习笔记(6)——探究活动(4)
Android:日常学习笔记(6)——探究活动(4) 活动的启动模式 standard模式 standard是活动默认的启动模式,在不进行显示定义的情况下,所有活动都会自动使用这种启动模式. stan ...
- Android动画学习笔记-Android Animation
Android动画学习笔记-Android Animation 3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...
- Android开发学习之LauncherActivity开发启动的列表
Android开发学习之LauncherActivity开发启动的列表 创建项目:OtherActivity 项目运行结果: 建立主Activity:OtherActivity.java [jav ...
- Android Fragement学习笔记(三)----PreferenceFragment的使用
相信大家对Perference都比較熟悉了,也就是我们常说的偏好设置,首选项设置,能够保存一些数据,比如我们在上一次使用的时候的一些内容,希望在下一次启动后依旧生效,而不须要再进行配置那么麻烦.一般这 ...
- Android Fragement学习笔记(三)----PreferenceFragment使用
我相信每个人都Perference大家都比较熟悉,这就是我们常说的偏好,首选项设置,一些数据可以保存.例如,一些元件的,我们使用上次.希望下次启动后仍然生效,烦.一般这个时候我们便会使用perfere ...
- Android:日常学习笔记(6)——探究活动(3)
Android:日常学习笔记(6)——探究活动(3) 活动的生命周期 返回栈 Android中的活动是可以叠加的,我们每启动一个新活动,就会覆盖在原来的活动上,点击Back以后销毁最上面的活动,下面的 ...
- Android:日常学习笔记(5)——探究活动(2)
Android:日常学习笔记(5)——探究活动(2) 使用Intent在活动之间穿梭 什么是Intent Intent时Android程序中各组件之间进行交互的一种重要方式,他不仅可以指明当前组件想要 ...
- Android:日常学习笔记(2)——分析第一个Android应用程序
Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...
随机推荐
- Tomcat安全
一.版本安全 升级当前的tomcat版本为最新稳定版本.故名思议,最新稳定版本就要兼顾最新和稳定这两个概念.一个稳定的版本,是需要时间沉淀的,而最新又是相对于稳定版而言的最新.因此我们一般会选择当前大 ...
- 别做操之过急的”无效将军”,做实实在在的”日拱一卒” zz
别做操之过急的”无效将军”,做实实在在的”日拱一卒” 前天在网上看到一句话很不错,拿来和大家分享,同时用我的“大叔”三观来解读这句话. 这句话是:“我们不需要操之过急的”无效将军”,我们需要实实在在的 ...
- bzoj 3263 陌上花开(cdq分治,BIT)
[题意] 求满足Ai<=Aj,Bi<=Bj,Ci<=Cj的数对的数目. [思路] cdq分治 借网上一句话:第一维排序,第二维cdq分治,第三维树状数组维护. 首先合并三维都是相 ...
- TreeView节点
TreeView由节点构成,建树通过对TreeView.items属性进行操作.Items是一个TTreeNodes对象,这是一个TTreeNode集. 一.针对TTreeNodes,也就是 Tree ...
- mysql登陆报错(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2))
部署mysql版本信息 version: 5.6.21 具体现象: mysql服务能够正常启动如下: [root@localhost ~]# service mysqld restart Shutti ...
- hdoj 1856 More is better【求树的节点数】
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- [iOS基础控件 - 3.4] 汤姆猫
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...
- 转载MSDN 在ASP.NET 中执行 URL 重写
转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...
- ISE和Modelsim联合仿真(转)
相信很多人会遇到过这个问题,不知如何让ISE调用Modelsim进行仿真.我也迷糊了不少时间,查查找找,终于弄明白了,所以有了本文,和大家分享一下.我尽量讲得详细点儿,多多上图. 我的环境:Windo ...
- Visual C#使用DirectX实现视频播放
Visual C#使用DirectX实现视频播放 visual|视频播放 - 很多人第一次接触到DirectX大都是通过游戏,至于安装.升级DirectX的原因无非是满足游戏运行的需要.Direct ...