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.
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
SecondActivityand click Finish
This will auto-generate both theSecondActivityclass and its corresponding layout fileactivity_second.xml.
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>
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); }); } }
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

