4.23

所花时间(包括上课):1

打码量(行):300

博客量(篇):1

了解到知识点:学习Component name属性

 

 package com.example.myapp;

 

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

 

public class MainActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

 

        // 获取按钮视图

        Button explicitButton = findViewById(R.id.explicit_button);

 

        // 设置显式跳转按钮点击监听器

        explicitButton.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                // 创建显式Intent,指定目标Activity

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

                // 启动目标Activity

                startActivity(intent);

            }

        });

    }

}

package com.example.myapp;

 

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

 

public class SecondActivity extends AppCompatActivity {

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_second);

    }

}

// 在MainActivity.java中添加一个用于隐式跳转的按钮

Button implicitButton = findViewById(R.id.implicit_button);

 

// 设置隐式跳转按钮点击监听器

implicitButton.setOnClickListener(new View.OnClickListener() {

    @Override

    public void onClick(View v) {

        // 创建隐式Intent,指定动作和数据

        Intent intent = new Intent(Intent.ACTION_VIEW);

        intent.setData(Uri.parse("https://www.example.com"));

        // 启动目标Activity

        startActivity(intent);

    }

});

<!-- activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="16dp">

 

    <!-- 显式跳转按钮 -->

    <Button

        android:id="@+id/explicit_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="显式跳转"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="50dp" />

 

    <!-- 隐式跳转按钮 -->

    <Button

        android:id="@+id/implicit_button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="隐式跳转"

        android:layout_centerHorizontal="true"

        android:layout_below="@id/explicit_button"

        android:layout_marginTop="20dp" />

</RelativeLayout>

<!-- AndroidManifest.xml -->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.myapp">

 

    <application

        android:allowBackup="true"

        android:label="@string/app_name"

        android:icon="@mipmap/ic_launcher"

        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>

 

</manifest>

posted @ 2024-04-23 21:44  赵千万  阅读(2)  评论(0编辑  收藏  举报