当前位置:   article > 正文

Android Studio实现页面跳转

Android Studio实现页面跳转

 建立文件

 

temp.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="hello"
  10. android:textSize="50dp" />
  11. </LinearLayout>

 activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/btn"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content" />
  10. </LinearLayout>

 MainActivity 

  1. package com.example.myapplication;
  2. import androidx.annotation.Nullable;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. public class MainActivity extends AppCompatActivity {
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. Button btn= (Button) findViewById(R.id.btn);
  15. btn.setOnClickListener(new View.OnClickListener() {
  16. @Override
  17. public void onClick(View v) {
  18. Intent intent=new Intent(MainActivity.this,MyTextView.class);
  19. startActivity(intent);
  20. }
  21. });
  22. }
  23. }

 MyTextView 

  1. package com.example.myapplication;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.util.AttributeSet;
  5. import android.widget.TextView;
  6. import androidx.annotation.Nullable;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class MyTextView extends AppCompatActivity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.temp);
  13. }
  14. }

 "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时:

  1. Intent intent=new Intent(MainActivity.this,OtherActivity.class);
  2. 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种

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

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

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

其中context含义

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/464861
推荐阅读
相关标签
  

闽ICP备14008679号

        
cppcmd=keepalive&