iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现页面跳转
  • 258
分享到

Android实现页面跳转

2024-04-02 19:04:59 258人浏览 薄情痞子
摘要

本文实例为大家分享了Android实现页面跳转的具体代码,供大家参考,具体内容如下 一. Android实现页面跳转有两种方式,一种为.MainActivity跳转;第二种是Rela

本文实例为大家分享了Android实现页面跳转的具体代码,供大家参考,具体内容如下

一. 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. 这是第一个页面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<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. 这是第二个页面的布局文件

<?xml version="1.0" encoding="utf-8"?>
<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="#663399"
        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);
    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<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)、标题布局


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);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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>

2)、中间布局


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);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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>

3)、底部布局


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);
    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<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的子类的名字。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android实现页面跳转

本文链接: https://www.lsjlt.com/news/151721.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android 实现页面跳转
    android使用Intent来实现页面跳转,Intent通过startActivity(Intent intent)或startActivityForResult(Intent intent,int resquestCode)方法来启动A...
    99+
    2023-05-30
    android 页面 跳转
  • Android实现页面跳转
    本文实例为大家分享了Android实现页面跳转的具体代码,供大家参考,具体内容如下 一. Android实现页面跳转有两种方式,一种为.MainActivity跳转;第二种是Rela...
    99+
    2024-04-02
  • android如何实现页面跳转
    Android中实现页面跳转主要有两种方式:隐式跳转和显式跳转。1. 隐式跳转:隐式跳转是指通过指定Intent的Action来进行...
    99+
    2023-08-19
    android
  • Android Studio 点击按钮实现页面跳转、网页跳转
    页面跳转、网页跳转 1)页面跳转 Btn1=findViewById(R.id.btn_1); Btn1.setOnClickListener(new View.OnClickListe...
    99+
    2023-09-17
    android studio android kotlin
  • jquery实现登陆跳转页面跳转页面跳转
    在Web开发中,很常见的一种需求是用户通过输入账号和密码完成登陆操作后,跳转到不同的页面。这一过程中需要用到Javascript库中非常流行的jQuery来实现。jQuery是一个快速、简洁的JavaScript库,其设计思想是“写更少,做...
    99+
    2023-05-25
  • android 3d页面跳转
    package cn.com; import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGr...
    99+
    2023-01-31
    跳转 页面 android
  • android studio实现简单的页面跳转
    运用intent组件实现简单的跳转 主页面 Button button1,button2,button3; //xml文件定义的id @Override protected void onCreate(Bundle savedI...
    99+
    2023-10-11
    android studio android java
  • Android Studio怎么实现注册页面跳转登录页面
    今天小编给大家分享一下Android Studio怎么实现注册页面跳转登录页面的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了...
    99+
    2023-06-30
  • android studio实现页面跳转(点击按钮)
    在已经创建的java文件MainActivity(点击app,点击java)下里面编写  package com.example.myapplication1120;import android.content.Intent;import ...
    99+
    2023-10-09
    android studio android ide
  • PHP页面跳转教程:如何实现页面跳转到新页面
    标题:PHP页面跳转教程:如何实现页面跳转到新页面,需要具体代码示例 在Web开发中,页面跳转是一个常见的操作,通过页面跳转可以实现用户在不同页面间进行流畅的切换,提升用户体验和网站功...
    99+
    2024-03-04
    教程 php 页面跳转 a标签
  • Android实现页面跳转的全过程记录
    目录1、启动新Activty1.1、功能分析1.2、开发视图布局1.3、按钮事件响应1.4、测试结果2、启动其他App2.1、功能分析2.2、开发视图布局2.3、按钮事件响应2.4、...
    99+
    2024-04-02
  • android怎么实现点击按钮跳转页面
    Android中实现点击按钮跳转页面可以通过以下步骤实现:1. 在XML布局文件中定义一个按钮组件,例如:```xml```2. 在Java代码中获取按钮组件,并设置点击事件监听器,例如:```javaButton button = f...
    99+
    2023-08-11
    android
  • Nignx实现页面跳转(rewrite)
    引言:Nginx作为前端反向代理的首选,在实际应用中还是会进行很多跳转,虽然Apache也可以做跳转,但是Nginx的跳转效率会更高。 一、Nginx跳转实现的方式 ①使用rewrite进行匹配跳转 主要是匹配的具体路径 ②使用if匹配全局...
    99+
    2023-09-06
    nginx php 服务器
  • Android实现注册页面(携带数据包跳转)
    安卓学习:实现注册页面输入数据,点击注册按钮跳转到另一个页面并显示输入信息 效果: 实现 1.创建安卓文件 2.创建注册界面,勾选为启动页 3.编写代码 启动界面activi...
    99+
    2024-04-02
  • javascript如何实现跳转页面
    JavaScript是一种常用的编程语言,常常用于网页开发中。在网页设计中,经常要用到跳转页面的功能,在这个过程中,JavaScript跳转页面是最常用的方法之一。JavaScript还可以帮助用户优化页面,增强用户的体验。本文将为您介绍使...
    99+
    2023-05-14
  • thinkphp怎么实现跳转页面
    这篇“thinkphp怎么实现跳转页面”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“thinkphp怎么实现跳转页面”文章吧...
    99+
    2023-07-05
  • react怎么实现跳转页面
    本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。react怎么实现跳转页面?react项目实现页面跳转更新:useNavigate()的使用import { useNavigate } from ...
    99+
    2023-05-14
    React
  • php如何实现页面跳转
    这篇文章将为大家详细讲解有关php如何实现页面跳转,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。php实现页面的三种跳转方法:1、通过header函数实现跳转;2、使用Meta标签实现跳转;3、通过在PH...
    99+
    2023-06-14
  • react如何实现跳转页面
    这篇文章主要介绍“react如何实现跳转页面”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“react如何实现跳转页面”文章能帮助大家解决问题。react实现跳转页面的方法:1、通过“import {...
    99+
    2023-07-04
  • PyQt5designer页面点击按钮跳转页面实现
    目录使用 designer 进行开发PyQt5 实现两个页面跳转PyQt5 实现多个页面跳转补充:使用 designer 开发,页面跳转函数不使用 designer 的页面跳转使用 ...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作