安卓四大组件----Activity组件---Activity参数传递

105 阅读1分钟

1.新建一个新项目:MyActivityImplicit 2.创建一个MyActivity.java:

public class MyActivity extends AppCompatActivity {

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

3.新建一个布局文件activity_my.xml: 设置控件排布方式:android:orientation="vertical"

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

    <TextView
        android:id="@+id/tv_msg"//设置一个id值
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="MyActivity"
        android:textSize="30dp">
    </TextView>

</LinearLayout>

那么下面主要实现的就是主页面设置一个按钮,点击按钮后跳转到我们 刚刚自己写的页面。

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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="隐式启动"
        android:onClick="startImplicit">
    </Button>
</LinearLayout>

5.接下来配置一下清单文件: 在这里插入图片描述

<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=".MyActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="MyActivity" />//自己起的名字
                <data android:scheme="http" android:mimeType="html/type"></data>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

6.设置主页面的实现类MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

    public void startImplicit(View view) {

        Intent intent=new Intent();
        intent.setAction("MyActivity");
        startActivity(intent);
    }
}

目前实现的效果如下:

在这里插入图片描述

在这里插入图片描述