Android实现页面跳转

Android实现页面跳转一 Android 实现页面跳转有两种方式 一种为 MainActivity 跳转 第二种是 Relatelayout 布局跳转 首先看第一种方式 1 MainActivity 区域设置 publicclassM Overrideprot BundlesavedI super onCreate savedInstanc

一. Android实现页面跳转有两种方式,一种为.MainActivity跳转;第二种是Relatelayout布局跳转,首先看第一种方式

1. MainActivity区域设置

public class MainActivity extends AppCompatActivity { 
    @Override protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取按钮 Button button = findViewById(R.id.button); //按钮进行监听 button.setOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View v) { 
    //监听按钮,如果点击,就跳转 Intent intent = new Intent(); //前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面 intent.setClass(MainActivity.this,NextActivity.class); startActivity(intent); } }); } } 

2. 这是下一个页面 的设置

public class NextActivity extends AppCompatActivity { 
    @Override protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); //这个是获取布局文件的,这里是你下一个页面的布局文件 setContentView(R.layout.activity_next); } } 

3. 这是第一个页面的布局文件

 
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/one" android:layout_width="200dp" android:layout_height="100dp" android:text="这是第一个页面!" android:textSize="25dp" android:layout_centerInParent="true" /> <Button android:id="@+id/button" android:layout_width="100dp" android:layout_height="50dp" tools:ignore="MissingConstraints" android:text="跳转" android:layout_centerHorizontal="true" android:layout_below="@+id/one" />  
     RelativeLayout>  
      androidx.constraintlayout.widget.ConstraintLayout> 

4. 这是第二个页面的布局文件

 
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是第二个页面!" android:textSize="25dp" android:textColor="#" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />  
     androidx.constraintlayout.widget.ConstraintLayout> 

5. AndroidManifest.xml配置加上第二个页面的入口
在这里插入图片描述
6. 效果图
在这里插入图片描述
在这里插入图片描述
一. 第二种方式是通过控制Java布局文件进行布局组合










1. 首先MainActivity文件

public class MainActivity extends AppCompatActivity { 
    / * 声明布局文件 * */ RelativeLayout layoutTitle,layoutBox,layoutButton; @Override protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取布局文件 getwige(); } / * 获取总体布局 * */ private void getwige() { 
    //获取标题布局 getTitles(); //获取中间布局 getBoxs(); //获取底部布局 getButtons(); } / * 获取标题布局 * */ public void getTitles(){ 
    //获取总布局中的标题布局 layoutTitle = this.findViewById(R.id.title); //初始化一个标题布局类 Titles title = new Titles(this); //进行组合布局 layoutTitle.addView(title); } / * 获取标题布局 * */ public void getBoxs(){ 
    //获取总布局中的中间布局 layoutBox = this.findViewById(R.id.box); //初始化一个中间布局类 Box box = new Box(this); //进行组合布局 layoutBox.addView(box); } / * 获取标题布局 * */ public void getButtons(){ 
    //获取总布局中的底部布局 layoutButton = this.findViewById(R.id.button); //初始化一个底部布局类 Buttons buttons = new Buttons(this); //进行组合布局 layoutButton.addView(buttons); } } 

其相对的主要布局文件如下:

 
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/title" android:layout_width="match_parent" android:layout_height="60dp" /> <RelativeLayout android:id="@+id/box" android:layout_width="match_parent" android:layout_height="590dp" android:layout_above="@+id/button" android:layout_below="@+id/title" /> <RelativeLayout android:id="@+id/button" android:layout_width="match_parent" android:layout_height="80dp" android:layout_alignParentBottom="true" />  
     RelativeLayout>  
      androidx.constraintlayout.widget.ConstraintLayout> 

2. 首先其他的一些组合布局的类以及其相对布局文件

  1. 标题布局
/ * author:LZH * Date: 2020/6/9 * ClassName:Title * Intruduce:标题布局类 */ public class Titles extends RelativeLayout { 
    public Titles(Context context) { 
    super(context); View.inflate(context, R.layout.activity_title,this); } public Titles(Context context, AttributeSet attrs) { 
    super(context, attrs); View.inflate(context, R.layout.activity_title,this); } public Titles(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); View.inflate(context, R.layout.activity_title,this); } } 

布局文件:

 
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="60dp" tools:context=".MainActivity"> <RelativeLayout android:id="@+id/title" android:layout_width="match_parent" android:layout_height="60dp" tools:ignore="MissingConstraints" android:background="#CCFF00"> <TextView android:layout_width="120dp" android:layout_height="30dp" android:layout_centerInParent="true" android:textSize="20dp" android:text="这个是标题" />  
     RelativeLayout>  
      androidx.constraintlayout.widget.ConstraintLayout> 
  1. 中间布局
/ * author:LZH * Date: 2020/6/9 * ClassName:Box * Intruduce:中间布局类 */ public class Box extends RelativeLayout { 
    public Box(Context context) { 
    super(context); View.inflate(context, R.layout.activity_box,this); } public Box(Context context, AttributeSet attrs) { 
    super(context, attrs); View.inflate(context, R.layout.activity_box,this); } public Box(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); View.inflate(context, R.layout.activity_box,this); } } 

布局文件:

 
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:id="@+id/box" android:layout_width="match_parent" android:layout_height="590dp" tools:ignore="MissingConstraints" android:background="#6600"> <TextView android:layout_width="150dp" android:layout_height="590dp" android:layout_marginTop="450dp" android:layout_centerInParent="true" android:textSize="20dp" android:text="这个是中间布局" />  
     RelativeLayout>  
      androidx.constraintlayout.widget.ConstraintLayout> 
  1. 底部布局
/ * author:LZH * Date: 2020/6/9 * ClassName:Button * Intruduce:底部布局类 */ public class Buttons extends RelativeLayout { 
    public Buttons(Context context) { 
    super(context); View.inflate(context, R.layout.activity_button,this); } public Buttons(Context context, AttributeSet attrs) { 
    super(context, attrs); View.inflate(context, R.layout.activity_button,this); } public Buttons(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); View.inflate(context, R.layout.activity_button,this); } } 

布局文件:

 
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <RelativeLayout android:id="@+id/box" android:layout_width="match_parent" android:layout_height="80dp" tools:ignore="MissingConstraints" android:background="#ccff"> <TextView android:layout_width="150dp" android:layout_height="30dp" android:layout_centerInParent="true" android:textSize="20dp" android:text="这个是底部布局" />  
     RelativeLayout>  
      androidx.constraintlayout.widget.ConstraintLayout> 

总结,其中第一中方法是真正的跳转方法,而第二中相对于一种组合布局,前者要用到两个或者多个Activity的子类,而后者只需要一个MainActivity。另外,在存在多个Activity的子类时需要设置多个入口,也就是

<activity android:name=".NextActivity"/> 

其中,“.”后面是你Activity的子类的名字。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/218323.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午7:31
下一篇 2026年3月18日 上午7:32


相关推荐

  • 空格的正则表达式

    空格的正则表达式在正则表达式想使用空格的时候不能采用\s的方法,因为\s指的是空白,就是所有空白。如果想表示单纯的空格的话可以采用:[]方括号本身就是匹配其中的字符,那么其中放空格就是匹配空格;如果有其他正则表达式问题可以查看:https://blog.csdn.net/cao849861802/article/details/102505834…

    2025年11月28日
    10
  • C#开发WebService实例和发布

    C#开发WebService实例和发布WebService是一种跨平台,独立于编程语言之外的通信技术,将其部署在服务器端,在网络内的所有设备都可以调用这个WebService。本文介绍用C#开发WebService实例以及其部署和调用。首先是一个简单的WebService工程的创建:1、创建一个asp.netweb应用程序,我这里起名字叫WebServiceTest2、创建一个空的模板3、然后给这个空项目添加web服务…

    2022年7月21日
    11
  • Pytest(6)重复运行用例pytest-repeat「建议收藏」

    Pytest(6)重复运行用例pytest-repeat「建议收藏」前言平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,

    2022年7月28日
    6
  • OPC和DCOM配置

    OPC和DCOM配置系统 使用 win1064 位系统 PDF 文件本文 链接 百度网盘密码 reht Win7 和 Win7 SP1 网络 OPC 配置 链接 百度网盘密码 dhhc 在线参考 英文的网上参考 1 网上参考 2 使用的 OPCServer 服务器 KEPServerV6 链接 百度网盘密码 ykj21 安装 OPC 运行库 KEPServer 集成了 OPC 运行库 所以

    2026年3月17日
    2
  • 动态规划之01背包问题(最易理解的讲解)[通俗易懂]

    动态规划之01背包问题(最易理解的讲解)[通俗易懂]01背包问题,是用来介绍动态规划算法最经典的例子,网上关于01背包问题的讲解也很多,我写这篇文章力争做到用最简单的方式,最少的公式把01背包问题讲解透彻。01背包的状态转换方程 f[i,j]=Max{f[i-1,j-Wi]+Pi(j>=Wi), f[i-1,j]}f[i,j]表示在前i件物品中选择若干件放在承重为j的背包中,可以取得的最大价值。Pi表示第i件物

    2022年7月26日
    5
  • HttpCanary教程_jquery post json

    HttpCanary教程_jquery post jsonHttpResponse对象Django服务器接收到客户端发送过来的请求后,会将提交上来的这些数据封装成一个HttpRequest对象传给视图函数。那么视图函数在处理完相关的逻辑后,也需要返回一个响

    2022年8月7日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号