•Intent 简介

  Intent 是 Android 程序中各组件之间进行交互的一种重要方式;

  它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据。

  Intent 有多个构造函数,其中一个是 Intent(Context packageContext, Class<?> cls):

    • 第一个参数 Context 要求提供一个启动活动的上下文
    • 第二个参数 Class 则是指定想要启动的目标活动

  通过这个构造函数可以构建出 Intent 的 “意图”;

•设置跳转界面

  新建一个 Empty Activity,命名为 AnotherActivity;

  修改 activity_another.xml 中的代码;

activity_another.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is Another Activity!"
android:textSize="20sp"
android:textColor="@color/black"/> </RelativeLayout>

  该代码只放置了一个用于显示文本的 TextView;

•跳转前的准备工作

  在 activity_main.xml 中创建一个 Button 控件,并设置其 id 为 btn,代码如下;

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="跳转"
/> </RelativeLayout><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="跳转"
/> </RelativeLayout>

•使用 Intent 完成跳转

  接下来修改 MainActivity.java 中的代码;

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Button mBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mBtn = findViewById(R.id.btn);
mBtn.setOnClickListener(new View.OnClickListener(){ @Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
startActivity(intent);
}
});
}
}

  在 MainActivity.java 中创建一个 Button 变量 mBtn;

  通过 findViewById(R.id.btn) 找到 btn 这个组件;

  创建一个新的 Activity,姑且命名为 nextActivity;

  然后,通过为 mBtn 设置点击事件完成界面跳转;

mBtn.setOnClickListener(new View.OnClickListener(){

    public void onClick(View view){
Intent intent=new Intent(MainActivity.this,AnotherActivity.class);
startActivity(intent);
} });

  通过  Intent intent=new Intent(); ,我们构造出了一个 Intent;

  传入 MainActivity.this 作为上下文,传入 AnotherActivity.class 作为活动目标;

  这样我们的意图就非常明显了:在 MainActivity 这个活动的基础上打开 AnotherActivity 这个活动;

  然后通过 startActivity() 方法来执行这个 Intent。

•运行效果

  

•其他跳转方式

  除了使用  Intent intent=new Intent(MainActivity.this,AnotherActivity.class); 完成跳转外,

  我们还可以这么写:

        mBtn.setOnClickListener(new View.OnClickListener(){

            @Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,AnotherActivity.class);
startActivity(intent);
}
});

  通过  intent.setClass() 方法完成跳转,效果是一样的;

Android Studio 之 通过 Intent 完成点击按钮实现页面跳转的更多相关文章

  1. jsp 中实现点击按钮 实现页面跳转到HTML

    <input type ="button" value="跳转" onclick="window.location.href='main.htm ...

  2. asp.net 点击按钮,页面没有任何变化,后台代码不触发

    asp.net 点击按钮,页面没有任何变化,后台代码不触发 和可能是 asp.net button  缺少validationGroup 导致的,需要查看页面的validation并且让他们抛出错误信 ...

  3. 用android studio创建第一个安卓程序加载html5 页面

    前言 软件版本:android studio v1.0正式版,由于v0.x以来软件变化一直比较大,很多问题搜索的解决方案也都是v0.x版本时代的,故首先声明一下版本. 动机:由于工作中需要对移动端软件 ...

  4. 家庭记账本app进度之对于登录和注册两个界面点击按钮的相互跳转

    这次主要完成了两个两个android页面之间的跳转.从登录页面点击注册就会跳转到注册页面.在注册页面点击返回登录,这样就可以返回到登录界面.主要是这样的操作.其中遇到了一个困难主要是当点击按钮的时候, ...

  5. jQuery 点击按钮刷新页面

    //页面加载时绑定按钮点击事件 $(function () { $("#按钮id").click(function () { refresh(); }); }); //点击按钮调用 ...

  6. 点击Button按钮实现页面跳转

    1.首先我们新建一个带有button按钮的页面 <button type="submit" class="form-contrpl">注册</ ...

  7. Vue -- element-ui el-table 点击tr项页面跳转,返回后缓存回显点击项

    页面跳转反显(点击项,点击table滚动的位置,搜索条件,分页回显) 点击table tr项后,页面跳转到下级页面,返回回显搜索条件.当前页码.并将点击项select选中.滚动条也被记录回显跳转时滚动 ...

  8. Android Studio学习随笔-基本事件(点击)

    最常见的点击事件有三种创建方法,在MainActivity.java的onCreate函数(在启动程序是优先运行的程序)中创建setOnClickListener(动态运行)(最常见) protect ...

  9. android studio使用真机测试时点击Debug调试模式时报Error running app:No target device found,点击运行模式却是启动正常的

    原因是adb没检测到设备(包括真机和虚拟机). 在Terminal执行adb devices命令,查看有没有连接到的设备. 如果没有设备,确认虚拟机是否正确打开,真机是否连接打开USB调试并安装驱动. ...

随机推荐

  1. Next.js Conf 2020

    Next.js Conf 2020 Next.js Conf Ticket https://nextjs.org/conf Conf Schedule https://confs.tech/javas ...

  2. 【转】ROS之topic和service通信比较

    实验速度 1. via topic 上图是以前ROS课上做的一个实验,内容是测试一个publisher和一个subscriber之间通讯所用的时间.两个node都很简单,publisher发送一个字符 ...

  3. InnoDB -- 行记录格式

    本文转载自InnoDB -- 行记录格式 分类 Named File Format InnoDB早期的文件格式(页格式)为Antelope,可以定义两种行记录格式,分别是Compact和Redunda ...

  4. 鸿蒙Java开发模式11:鸿蒙图片裁剪功能的实现

    鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口] 目录: 1. 鸿蒙版图片裁剪功能效果展示 2.Java代码实现 3.裁剪工具类实现 4.<鸿蒙Java开发模式>系 ...

  5. SpringBoot使用谷歌方式生成图片验证码

    1.新建一个springboot的项目 2.导入坐标 <dependency> <groupId>com.github.penggle</groupId> < ...

  6. 答不上的JUC笔试题

    1:有一个总任务A,分解为子任务A1 A2 A3 ...,任何一个子任务失败后要快速取消所有任务,请写程序模拟. 「请寻求最优解,不要只是粗暴wait()」 本题解题思路:Fork/Join 通常使用 ...

  7. elasticsearch如何设计集群

    本文为博客园作者所写: 一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 在写本文时就在想,如果让你负责一个elasticsearch集群,从零开始,你会从哪些方面 ...

  8. 探索 .NET Core 依赖注入的 IServiceProvider

    在上一篇文章中,我们学习了Microsoft.Extensions.DependencyInjection中的IServiceCollection,包括服务注册转换为ServiceDescriptor ...

  9. 将MacOS Catalina 降级为 Mojave

    1.下载Mojave https://apps.apple.com/cn/app/macos-mojave/id1398502828?ls=1&mt=12 2.更改U盘格式和名称 3.制作U盘 ...

  10. 《C++ Primer》笔记 第4章 表达式

    C++的表达式要不然是右值(right-value or read-value),要不然就是左值(left-value or location-value). 当一个对象被用作右值的时候,用的是对象的 ...