Android Studio实现页面跳转

server/2024/9/23 15:22:33/

 建立文件

 

temp.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hello"android:textSize="50dp" /></LinearLayout>

 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:id="@+id/btn"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>

 MainActivity 

package com.example.myapplication;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn= (Button) findViewById(R.id.btn);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(MainActivity.this,MyTextView.class);startActivity(intent);}});}}

 MyTextView 

package com.example.myapplication;import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.TextView;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;public class MyTextView extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.temp);}
}

 "ALT+ENTER"点击"Add activity to manifest"

然后点击按钮就可以切换页面了

原理

Intent用于Android程序中各组件(Activity、BroadcastReceive、Service)的交互,并且可以在组件之间传递数据,分为显式Intent和隐式Intent

Intent的中文意思为“意图”,在Android中可以理解为想要做什么,What do want to do? 所以什么时候要用到Intent就很好理解了。

通过Intent(Context packageContext, Class<?> cls)构造函数创建Intent实例,第一个参数为当前Context,第二个参数为要启动的目标类。如当需要启动OtherActivity时:

Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);

二.公共构造函数:

1、Intent() 空构造函数

2、Intent(Intent o) 拷贝构造函数

3、Intent(String action) 指定action类型的构造函数

4、Intent(String action, Uri uri) 指定Action类型和Uri的构造函数,URI主要是结合程序之间的数据共享ContentProvider

5、Intent(Context packageContext, Class<?> cls) 传入组件的构造函数,也就是上文提到的

6、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前两种结合体

多种构造函数本文用的是第5种

    /*** Create an intent for a specific component.  All other fields (action, data,* type, class) are null, though they can be modified later with explicit* calls.  This provides a convenient way to create an intent that is* intended to execute a hard-coded class name, rather than relying on the* system to find an appropriate class for you; see {@link #setComponent}* for more information on the repercussions of this.** @param packageContext A Context of the application package implementing* this class.* @param cls The component class that is to be used for the intent.** @see #setClass* @see #setComponent* @see #Intent(String, android.net.Uri , Context, Class)*/public Intent(Context packageContext, Class<?> cls) {mComponent = new ComponentName(packageContext, cls);}

为特定组件创建 Intent 。所有其他字段(操作、数据、类型、类)均为 null,但稍后可以使用显式调用对其进行修改 

packageContext – 实现此类的应用程序包的上下文。 cls – 要用于目的的组件类。 

其中context含义

Context,字面意思:语境、环境、上下文,在 Android 系统中,可以理解为当前对象在应用程序中所处的工作环境

内部定义很多访问应用程序环境中全局信息的接口,通过它可以访问到应用程序的资源有关的类,如:ResourcesAssetManagerPackage 及权限相关信息等。还可以通过它调用应用程序级的操作,如:启动 Activity 和 Service发送广播等。

/*** Interface to global information about an application environment.  This is* an abstract class whose implementation is provided by* the Android system.  It* allows access to application-specific resources and classes, as well as* up-calls for application-level operations such as launching activities,* broadcasting and receiving intents, etc.*/
public abstract class Context {...}

与应用程序环境的全局信息的接口。 这是一个抽象类,其实现由 Android 系统提供。 它
 允许访问特定于应用程序的资源和类,以及
 对应用程序级操作(如启动活动)的上行调用,
 广播和接收意图等


http://www.ppmy.cn/server/15191.html

相关文章

C++学习第九天(list及其模拟实现)

1、list介绍 list是可以在常熟范围内任意位置进行 插入和删除的序列式容器&#xff0c;并且该容器可以前后双向迭代list的底层是双向链表结构&#xff0c;双向链表中每个元素存储在互不相关的独立节点中&#xff0c;在节点中通过指针指向其前一个元素和后一个元素list和forward…

【UI】element-ui的el-dialog的遮罩层在模态框的前面bug

最近在写element ui 的时候使用dialog组件&#xff0c;偶然出现了这种情况 原因&#xff1a; 是因为遮罩层插入进了body标签下&#xff0c;z-index高于当前父元素。 解决&#xff1a;在el-dialog标签里加上:modal-append-to-body"false"就可以了。 饿了么官网文档&a…

六个月滴滴实习:轻松、舒心又高薪!

不久前&#xff0c;一位在滴滴后端研发部门实习了六个月的小伙伴在牛客网上分享了他的实习体验&#xff0c; 作者详细描述了他在滴滴的实习生活。 从他的叙述中&#xff0c;我们可以感受到与其他互联网公司相比&#xff0c;滴滴的工作环境显得相对轻松和舒适。 他提到&#x…

C语言中的动态内存管理

1. **malloc函数**&#xff1a;这是C语言中用于动态分配内存的函数。它接受一个参数&#xff0c;即所需内存的大小&#xff08;以字节为单位&#xff09;&#xff0c;并返回一个指向新分配内存块的指针。如果分配成功&#xff0c;返回的指针可以用于访问这块内存&#xff1b;如…

垃圾收集器ParNewCMS与底层三色标记算法详解

垃圾收集算法 分代收集理论 当前虚拟机的垃圾收集都是采用分代收集算法,这种算法没有什么新思想,只是依据对象的存活周期不同将内存分为几块.一般将Java堆分为新生代和老年代,这样就可以根据各个年代的特点选择合适的垃圾收集算法. 比如在新生代中,每次收集都会有大量对象(近…

[卷积神经网络]YoloV9

一、概述 代码路径为&#xff1a; YoloV9https://github.com/WongKinYiu/yolov9 YoloV9的作者在论文中指出&#xff1a;现在的深度学习方法大多都在寻找一个合适的目标函数&#xff0c;但实际上输入数据在进行特征提取和空间变换的时候会丢失大量信息。针对这个问题&#xff…

前端表单input的简单使用

1.代码结构介绍 2.实战效果

每天五分钟计算机视觉:基于卷积操作完成滑动窗口的图片分类?

本文重点 我们前面学习了使用不同大小的滑动窗口来滑动图片,然后切分成许多小的图片,然后依次应用到我们已经训练好的图像分类模型中,但是这种方式效率太低了,本节课程我们学习一种新的方式,来看一下如何并行识别这些剪切的图片。 原始结构 首先我们先来看一下,如何把…