Android 实现页面之间跳转(显示意图)——小白一看就会

本文详细介绍了在Android应用中如何使用Intent实现页面间的跳转,包括Intent的基本用法、Activity的注册、页面跳转的代码实现及运行效果展示。
该文章已生成可运行项目,

Android 页面之间跳转——显示意图跳转

利用Intent进行跳转

Intent方法进行页面跳转适用于两个Activity之间的跳转,按返回键可以直接返回到前一个页面。

使用时需要注意在Manifest.xml文件中注册Activity

activity_main.xml(Mainactivity布局)

<?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"
   >
    <TextView
        android:id="@+id/tv_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一个页面"
        android:layout_gravity="center"
        android:textSize="40dp"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="onclick"
        android:text="进入第二个页面"
        />
</LinearLayout>

创建按钮的onclick方法

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick(View view) {
        Intent intent = new Intent(this,SecondActivity.class);
        startActivity(intent);
    }
}

Intent方法两个参数,第一个是上下文,第二个是跳转的Activity
新建Activity方法:
.new——activity——empty activity
在这里插入图片描述
随后会产生一个相应的布局文件

<?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"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个页面"
        android:layout_gravity="center"
        android:textSize="40dp"
        />


</LinearLayout>

如图是运行效果。点击按钮 跳转到第二个页面

在这里插入图片描述
在这里插入图片描述

Andriod项目默认生成一个MainActivity.java,如果执行,会默认执行它,但如果在别的Activity中写了登录界面,如何先执行登录呢?

假设是这种情况,打开/项目名/AndroidManifest.xml这个xml文件中

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SecondActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

就理解为过滤器,它指定了启动应用程序的Intent对象的动作和类型

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SecondActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity android:name=".MainActivity">
               <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

这样第一个启动的就是SecondActivity了

首个启动的设置为

<category android:name="android.intent.category.LAUNCHER" />

其他的设置为

<category android:name="android.intent.category.DEFAULT" />
本文章已经生成可运行项目
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值