以下是创建新项目的完整步骤,确保你创建一个使用传统 View 系统(XML 布局)的项目。

第一步:打开 Android Studio 并创建新项目

  1. 打开 Android Studio(如果你还没有打开的话)

  2. 点击 "New Project" 或在欢迎界面选择 "New Project"

第二步:选择项目模板

这是最关键的一步!请确保选择 "Empty Views Activity"

  1. 在模板选择界面,你会看到多个选项:

    • Phone and Tablet 部分

    • 选择 "Empty Views Activity"(不是 "Empty Activity")

    重要区别

    • ✅ "Empty Views Activity":使用传统 XML 布局(你的代码需要这个)

    • ❌ "Empty Activity":使用 Jetpack Compose

    https://developer.android.com/static/studio/images/new-project-views-activity_2x.png

  2. 点击 "Next"

第三步:配置项目

填写项目配置信息:

text

Name: MyFirstApplication
Package name: com.example.myfirstapplication  (可以保持不变)
Save location: 选择你喜欢的文件夹路径(默认即可)
Language: Kotlin  (确保选择这个)
Minimum SDK: API 24: Android 7.0 (Nougat)  (建议选择 24 或更高)

重要

  • ✅ 确保 "Use legacy android.support libraries" 选项 没有 被选中

  • ✅ 确保使用最新的构建配置

点击 "Finish"

第四步:等待项目构建

创建项目后,Android Studio 会自动:

  1. 下载必要的依赖

  2. 配置 Gradle

  3. 构建项目

这可能需要几分钟时间,取决于你的网络和电脑速度。

第五步:复制你的代码

项目创建完成后,打开以下文件并替换内容:

1. 替换 MainActivity.kt

找到文件:app/src/main/java/com/example/myfirstapplication/MainActivity.kt

替换为以下内容:

kotlin

package com.example.myfirstapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {

    // 定义可能的文本数组
    private val textArray = arrayOf(
        "你好,世界!",
        "文本已改变!",
        "Kotlin真棒!",
        "Android开发很有趣!",
        "这是新的文本内容!",
        "再次点击试试看!"
    )

    private var currentIndex = 0

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

        // 获取TextView和Button的引用
        val textView = findViewById<TextView>(R.id.textView)
        val button = findViewById<Button>(R.id.changeButton)

        // 为按钮设置点击监听器
        button.setOnClickListener {
            // 改变文本框的内容
            currentIndex = (currentIndex + 1) % textArray.size
            textView.text = textArray[currentIndex]

            // 可选:添加一些动画效果
            textView.animate()
                .scaleX(1.2f)
                .scaleY(1.2f)
                .setDuration(200)
                .withEndAction {
                    textView.animate()
                        .scaleX(1f)
                        .scaleY(1f)
                        .setDuration(200)
                }
        }
    }
}

2. 检查 activity_main.xml

找到文件:app/src/main/res/layout/activity_main.xml

确保它有以下内容(Android Studio 可能已经创建了类似内容):

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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/changeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果需要,你可以修改为之前提到的 LinearLayout 版本:

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="初始文本"
        android:textSize="24sp"
        android:layout_marginBottom="32dp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/changeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击改变文本"
        android:textSize="18sp"
        android:padding="16dp" />

</LinearLayout>

第六步:检查项目配置

1. 检查 build.gradle.kts (app)

打开 app/build.gradle.kts,它应该类似于:

kotlin

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    namespace = "com.example.myfirstapplication"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.example.myfirstapplication"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.11.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

2. 同步 Gradle

如果 Android Studio 提示 "Sync Now",点击它。或者:

  • File → Sync Project with Gradle Files

第七步:运行应用

  1. 连接设备或创建模拟器

    • 如果你有 Android 手机,连接电脑并启用 USB 调试

    • 或者点击工具栏上的 AVD Manager 图标创建模拟器

  2. 运行应用

    • 点击工具栏上的绿色运行按钮(▶)

    • 或按 Shift + F10

    • 选择你的设备或模拟器

    • 点击 "OK"

如果遇到问题:

1. 项目创建失败:

  • 检查网络连接

  • 确保 Android Studio 是最新版本

  • 尝试使用代理或 VPN

2. Gradle 同步失败:

  • File → Sync Project with Gradle Files

  • File → Invalidate Caches and Restart...

3. 运行失败:

  • 检查控制台输出错误信息

  • 确保设备/模拟器已正确连接

  • 检查 AndroidManifest.xml 是否有 MainActivity 声明

项目结构验证:

创建成功后,你的项目结构应该如下:

text

MyFirstApplication/
├── app/
│   ├── src/
│   │   ├── main/
│   │   │   ├── java/com/example/myfirstapplication/
│   │   │   │   └── MainActivity.kt
│   │   │   ├── res/
│   │   │   │   ├── layout/
│   │   │   │   │   └── activity_main.xml
│   │   │   │   ├── values/
│   │   │   │   └── ...
│   │   │   └── AndroidManifest.xml
│   ├── build.gradle.kts
│   └── ...
├── gradle/
├── gradlew
├── gradlew.bat
├── settings.gradle.kts
└── build.gradle.kts

成功标志:

  1. 应用能够成功构建(无红色错误信息)

  2. 应用能够安装到设备/模拟器

  3. 应用启动后显示 TextView 和 Button

  4. 点击按钮可以改变文本内容

如果按照这些步骤操作,你应该能够成功创建并运行一个简单的 Android 应用。如果还有问题,请分享具体的错误信息。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐