Activity---启动系统软件

61 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

我们知道Activity是安卓的四大组件之一,本篇博客就是学习组件Activity

一.实现android两个入口

1.我们新建一个工程MyActivity

2.创建一个新的入口MainActivity2:

在这里插入图片描述

3.继承自AppCompatActivity,并且重写onCreate方法

在这里插入图片描述

4.改一下布局文件activity_main.xml

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="入口1">
    </TextView>

</LinearLayout>

5.复制刚刚写的布局文件activity_main.xml,重新建一个新的布局文件activity_main2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="入口2">
    </TextView>
    
</LinearLayout>

然后把刚刚新建的入口实现类MainActivity2.java改成如下:

package com.example.myactivity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

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

6.然后我们还要配置一下清单文件

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myactivity">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyActivity"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity2"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

7.其实在刚刚的清单文件里也有很多属性,比如我们设置一下图标

8.我们选择两个图标照片放在mipmap文件夹下:

在这里插入图片描述 放置的位置如下所示: 在这里插入图片描述 在这里插入图片描述 9.下面我们测试运行一下,查看系统是否可以检测到这两个入口

点击下面的图标:

在这里插入图片描述 发现刚刚我们自己创建的两个入口系统可以检测到了:

在这里插入图片描述 我们点击最后边的图标,可以有如下的显示:

在这里插入图片描述 以上就是安卓系统实现两个入口啦!!!