You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio入门者求助:点击按钮跳转至下一个Activity的代码示例

Hey there! As someone who’s just starting out with Android Studio, I totally get how wrapping your head around Activity navigation can feel a bit overwhelming at first. Let’s walk through exactly how to make a button jump to another Activity, with full, copy-pasteable code examples for both Kotlin and Java.

Step 1: Create Your Second Activity

First things first—you need a destination Activity! In Android Studio:

  • Right-click your app’s package name (usually under app/src/main/java/com/yourdomain/yourapp)
  • Hover over New → Activity → select Empty Views Activity
  • Name it SecondActivity and click Finish
    This will auto-generate both the SecondActivity class and its corresponding layout file activity_second.xml.
Step 2: Add a Button to Your First Activity’s Layout

Open your main activity’s layout (usually activity_main.xml) and add a Button with a unique ID so we can reference it in code. Here’s a simple centered button using ConstraintLayout:

<!-- activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_navigate_to_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to Second Activity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Add the Click Listener to Launch the Second Activity

Now we need to tell the button what to do when it’s clicked. Below are examples for both Kotlin and Java—use whichever you’re working with!

Kotlin Version (MainActivity.kt)

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Find the button using its ID from the layout
        val navigateButton: Button = findViewById(R.id.btn_navigate_to_second)

        // Set a click listener to trigger the navigation
        navigateButton.setOnClickListener {
            // Create an Intent: this tells Android which Activity to start
            val intent = Intent(this, SecondActivity::class.java)
            
            // Optional: Pass data to the second Activity (example)
            intent.putExtra("USER_MESSAGE", "Hello from Main Activity!")
            
            // Launch the Second Activity
            startActivity(intent)
        }
    }
}

Java Version (MainActivity.java)

import android.content.Intent;
import android.os.Bundle;
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);

        // Find the button using its ID from the layout
        Button navigateButton = findViewById(R.id.btn_navigate_to_second);

        // Set a click listener to trigger the navigation
        navigateButton.setOnClickListener(v -> {
            // Create an Intent: this tells Android which Activity to start
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            
            // Optional: Pass data to the second Activity (example)
            intent.putExtra("USER_MESSAGE", "Hello from Main Activity!");
            
            // Launch the Second Activity
            startActivity(intent);
        });
    }
}
Step 4: Verify the Second Activity is Registered (Important!)

When you create a new Activity via Android Studio, it automatically adds an entry to your AndroidManifest.xml file—but it’s good to double-check! Make sure you see something like this inside the <application> tag:

<activity
    android:name=".SecondActivity"
    android:exported="false" />

android:exported="false" is safe here because we’re only launching this Activity from within our app. If you ever need to launch it from outside (like via a deep link), you’d set this to true.

Bonus: Retrieve Data in the Second Activity

If you added the optional extra data, here’s how to get it in SecondActivity:

Kotlin

// Inside SecondActivity's onCreate method
val message = intent.getStringExtra("USER_MESSAGE")
// You can then display this message in a TextView, for example

Java

// Inside SecondActivity's onCreate method
String message = getIntent().getStringExtra("USER_MESSAGE");

That’s all there is to it! Run your app, click the button, and you’ll jump straight to the second Activity. If you run into any issues, just double-check your IDs match between the layout and code, and that the Activity is registered in the manifest.

内容的提问来源于stack exchange,提问作者ASLAM C A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:57:37