两个活动之间的跳转要通过intent来进行,intent跳转分为隐式的和显示的。

首先xml中定义Button,通过按下按钮实现回调,在回调函数中进行相应intent设置。

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"
android:onClick="sendmessage"
/>

回调函数中:

显示调用:

public void sendmessage(View view)
{
//法一:
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);

//法二:
Intent intent=new Intent();
intent.setClassName(this,"com.example.helloworld.SecondActivity"); //第二个参数为包名中相应的activity
this.startActivity(intent);

//法三:
Intent intent=new Intent();
ComponentName componentName=new ComponentName(this,SecondActivity.class);
intent.setComponent(componentName);
startActivity(intent);

//法四:
Intent intent=new Intent();
ComponentName componentName=new ComponentName(this,"com.example.helloworld.SecondActivity");
intent.setComponent(componentName);
startActivity(intent);
}

//隐式调用:
public void sendmessage(View view)
{
Intent intent=new Intent();

intent.setAction("second.activity");
startActivity(intent);
}

AndroidMainifest.xml中:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="second.activity" />
        <category android:name="android.intent.category.DEFAULT" /> //如果这里也写了LAUNCHAR,那么用户可以直接访问第二个活动,也就是手机上会有两个应用。
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>


实现了活动间的跳转,如果想要在两个活动间发送数据,那么要用到Bundle:

以显示调用的一种跳转方法为例:

现有两个活动:activity1和activity2,实现activity1中的字符串发送到activity2
在activity1中:
    <EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</EditText>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onclick"
android:text="send"
></Button>

回调函数中:

public void onclick(View view)
{
EditText s=findViewById(R.id.edit);

Intent intent=new Intent();
ComponentName componentName=new ComponentName(this,Main4Activity.class);
intent.setComponent(componentName);

Bundle bundle=new Bundle();
bundle.putString(s.getText().toString());
intent.putExtras(bundle);

startActivity(intent);
}

在activity2中:
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
></TextView>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);

TextView s=findViewById(R.id.text);
Bundle bundle=this.getIntent().getExtras();
String str=bundle.getString("text");

s.setText(str);

}


安卓开发之intent的更多相关文章

  1. Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...

  2. android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序

    android开发之Intent.setFlags()_让Android点击通知栏信息后返回正在运行的程序     在应用里使用了后台服务,并且在通知栏推送了消息,希望点击这个消息回到activity ...

  3. 安卓开发之Toolbar

    根据官网的教程,发现实现与预期不一致,查看相关资料自己整理了一下(官网开发文档:https://developer.android.com/training/appbar/setting-up.htm ...

  4. 安卓开发之ListAdapter(二)

    今天我们来学习一下ArrayAdapter: ArrayAdapter是安卓中最简单的适配器.使用ArrayAdapter(数组适配器),需要把数据源存 放至数组里面来显示. •构造函数: publi ...

  5. 安卓开发之activity详解(sumzom)

    app中,一个activity通常是指的一个单独的屏幕,相当于网站里面的一个网页,它是对用户可见的,它上面可以显示一些控件,并且可以监听处理用户的时间做出响应. 那么activity之间如何进行通信呢 ...

  6. 安卓开发之UIwebview

    web view在安卓开发中是比较常见的UI,像微信的新闻模块就采用了这个,他的作用越来越广,下面我把以前做的贴了出来,如果有更好的办法,希望大神不吝赐教哈,嘿嘿,纯代码来了: java代码 publ ...

  7. Android开发之Intent略解

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

  8. Android开发之Intent的传值--Application

    每当我们想要将输入的值传递到多个界面时,只是使用Intent传值的话,就会有一些的弊端. 下面我就以三个页面为例,进行简单的说明一下: 思路: 1.第一个页面是客户输入相关的信息. 2.将客户输入的信 ...

  9. 安卓开发之viewpager学习(头条显示)

    activity_luancher.xml代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res ...

随机推荐

  1. UDP&串口调试助手用法(4)

    接收配置用法 概览 保存文件 可将数据保存到文件和文件夹 如果选择的时文件,则需要自己手动选择保存的文件. 如果选择的时文件夹,则需要指定文件夹的类型和文件的后缀 支持保存文件类型: 文本文件和二进制 ...

  2. AQS的原理及源码分析

    AQS是什么 AQS= volatile修饰的state变量(同步状态) +FIFO队列(CLH改善版的虚拟双向队列,用于阻塞等待唤醒机制) 队列里维护的Node节点主要包含:等待状态waitStat ...

  3. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  4. 【Android开发】微信精选,文章资讯类App开发记录总结

    缘起 微信精选的App开发来源是在聚合数据上看到了有免费的微信精选的数据接口,无限调用.相对于其他的诸如违章查询,医药查询,NBA赛事等等,我感觉还是微信文章精选这个数据接口离我最近,所以想着拿着个数 ...

  5. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  6. 【剑指Offer】数组中重复的数字 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 Set 快慢指针 日期 题目地址:https://leetcod ...

  7. A. Points on Line

    A. Points on Line time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. 问题--ImportError: DLL load failed: 找不到指定的模块

    今天在运行别人的项目时出现了问题: ImportError: DLL load failed: 找不到指定的模块. 解决方法: 卸载后重新安装. 详情参考: Python报错:ImportError: ...

  9. iNeuOS工业互联网操作系统,矿山动态产量计量系统和铁路车辆识别系统应用场景案例

    目       录 1.      概述... 2 2.      平台演示... 2 3.      矿山动态产量计量系统... 2 4.      铁路车辆识别系统... 4 1.   概述 iN ...

  10. 使用 Eclipse 创建一个静态的登录页面

    要求: 使用 Eclipse 创建一个静态的登录页面 实现步骤: 在 Eclipse 中,点击"File",显示菜单,选择"New" "Other&q ...