这里有一个layout资源,2个activity。首先在MainActivity.java中实例化按钮和添加按钮监听绑定都是我们知道的,这里要注意的是第22行Intent intent = new Intent(MainActivity.this, OtherActivity.class);用来创建一个意图,并设置需要打开的activity,然后在24行intent.putExtra("Main", "我是发送的数据~娃哈哈");发送消息,前一个string为消息名字,后面的为消息内容,最后启动另外一个activity.

 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);//退出程序
}
}
}

在另一个activity中第8~11行:用Intent intent = this.getIntent();得到当前activity的意图,然后根据名字获得名字的内容:String str = intent.getStringExtra("Main");这样我们就能实现activity和activity之间的消息传递啦~

 public class OtherActivity extends Activity {
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
setContentView(tv);
//得到当前Activity的意图
Intent intent = this.getIntent();
//获取数据
String str = intent.getStringExtra("Main");
//将获取到的数据设置成TextView的文本
tv.setText(str);
}
}

本文链接:http://www.cnblogs.com/zjutlitao/p/4230228.html

更多精彩:http://www.cnblogs.com/zjutlitao/p/

[安卓] 7、页面跳转和Intent简单用法的更多相关文章

  1. Vue页面跳转$router.push 的用法

    vue2.0在使用的过程中, .vue文件之间的跳转,在template中的常见写法是: <router-link to="/miniCard/statement/horizon&qu ...

  2. .Net程序猿玩转Android开发---(11)页面跳转

    在不论什么程序开发中,都会遇到页面之间跳转的情况,Android开发也不例外.这一节,我们来认识下Android项目中如何进行页面跳转.页面跳转分为有參数和无參数页面跳转,已经接受还有一个页面的返回值 ...

  3. [Android应用开发] 04.页面跳转和数据传输

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. Intent实现页面跳转和传值

    *Intent称为意图,是Android各大组件连接的桥梁 1.Activity页面跳转 同一个包内 Intent intent = new Intent(); intent.setClass(Mai ...

  5. Android成长日记-使用Intent实现页面跳转

    Intent:可以理解为信使(意图),由Intent来协助完成Android各个组件之间的通讯 Intent实现页面之间的跳转 1->startActivity(intent) 2->st ...

  6. Intent实现页面跳转

    Intent实现页面跳转: 1. startActivity(intent) 2. startActivityForResult(intent,requestCode); onActivityResu ...

  7. [转]使用storyboard实现页面跳转,简单的数据传递

    由于最近才接触到IOS,苹果已经建议storyboard来搭建所有界面了,于是我也追随时尚,直接开始使用storyboard.(不料在涉及到页面跳转的时候,遇到的问题是:点击后没有任何反应)众所周知, ...

  8. 使用storyboard实现页面跳转,简单的数据传递

    由于最近才接触到IOS,苹果已经建议storyboard来搭建所有界面了,于是我 也追随时尚,直接开始使用storyboard.(不料在涉及到页面跳转的时候,遇到的问题是:点击后没有任何反应)众所周知 ...

  9. 转:ECharts图表组件之简单关系图:如何轻松实现另类站点地图且扩展节点属性实现点击节点页面跳转

    站点地图不外乎就是罗列一个网站的层次结构,提炼地讲就是一个关系结构图.那么我们如何巧用ECharts图表组件内的简单关系结构图来实现一个站点的地图结构呢?另外如何点击某个节点的时候实现页面跳转呢? 针 ...

随机推荐

  1. delegate事件绑定

    为了代码的健壮性,绑定事件之前先解绑再进行绑定. var _$div = $("#id");_$div.undelegate("click mouseover mouse ...

  2. rotate the clock

    A program test: You are given N round clocks. Every clock has M hands, and these hands can point to ...

  3. Towers of Hanoi

    Your mission is to move the stack from the left peg to the right peg. The rules are well known: Only ...

  4. Delphi下使用Oracle Access控件组下TOraSession控件链接

    Delphi下使用Oracle Access控件组下TOraSession控件链接数据库,使用  orsn1.Options.Direct:=true;  orsn1.Server:=IP:Port: ...

  5. LightOJ 1313 - Protect the Mines(凸包)

    1313 - Protect the Mines   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 M ...

  6. Select查询语句2

    一.模糊查询 1.语法结构 select*from table_name where column like '%context%' 在使用like运算符时如果不使用通配符“%”,则like的作用与= ...

  7. Selenium2+python自动化10-登录案例

    前言 前面几篇都是讲一些基础的定位方法,没具体的案例,小伙伴看起来比较枯燥,有不少小伙伴给小编提建议以后多出一些具体的案例.本篇就是拿部落论坛作为测试项目,写一个简单的登录测试脚本. 在写登录脚本的时 ...

  8. (引用)web安全测试

    转载:http://www.51testing.com/html/44/15020244-908645.html Web安全测试之XSS XSS 全称(Cross Site Scripting) 跨站 ...

  9. constraint更新表列约束默认值

    --更新约束 alter TABLE [dbo].[Sk_Recruit] drop constraint DF_Sk_Recruit_lastcommenttime go   alter TABLE ...

  10. 修改myeclipse的servlet模板

    今天修改myeclipse的servlet模板时,发生 Could not create the view: An unexpected exception was thrown.错误. 解决方案:1 ...