给你整理了一份【一看就懂的 Android APP 开发入门教程】,包含从基础概念到实战项目的核心知识,适合零基础快速上手。
一看就懂的 Android APP 开发入门教程
目录
- Android 基础介绍
- 开发环境搭建
- Android 项目结构解析
- 常用控件与布局
- 事件处理
- Activity 生命周期
- 简单项目实战:Hello World
- 进阶知识点介绍
- 推荐学习资源
1. Android 基础介绍
- Android是什么? 基于 Linux 的开源移动操作系统
- APP开发方式: 原生开发(Java/Kotlin)、跨平台(Flutter、React Native)
- APK包: Android 应用安装包格式
2. 开发环境搭建
- 安装 Android Studio(官方IDE)
- 配置 JDK(Java Development Kit)
- 创建第一个项目
3. Android 项目结构解析
- manifest/AndroidManifest.xml:配置权限和应用声明
- java/:源代码目录
- res/:资源文件(布局xml、图片、字符串)
- build.gradle:构建脚本
4. 常用控件与布局
- 布局容器:LinearLayout、RelativeLayout、ConstraintLayout
- 常用控件:Button、TextView、EditText、ImageView、RecyclerView
- 控件属性:宽高(match_parent/wrap_content)、padding、margin
5. 事件处理
- 按钮点击事件
setOnClickListener
- 触摸事件
onTouchEvent
- 输入框监听
TextWatcher
6. Activity 生命周期
- 主要回调:
onCreate()
,onStart()
,onResume()
,onPause()
,onStop()
,onDestroy()
- 理解生命周期有助于管理资源和防止内存泄漏
7. 简单项目实战:Hello World
// MainActivity.java
package com.example.helloworld;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello World!");
setContentView(tv);
}
}
8. 进阶知识点介绍
- Intent(意图)用于界面跳转和数据传递
- Fragment 实现界面模块化
- RecyclerView 显示列表数据
- 网络请求(Retrofit、OkHttp)
- 数据存储(SharedPreferences、SQLite、Room)
- 权限申请(运行时权限)
9. 推荐学习资源
- Android 官方开发文档
- Google Codelabs
- [慕课网、极客时间、B站相关视频教程]
- 开源项目 GitHub 搜索 “android beginner”
好嘞!我帮你准备了一个超简单的 Android Studio HelloWorld 项目模板,用 Java 写的,方便你直接导入并运行,带注释讲解。
1. 项目结构说明
HelloWorldApp/
├── app/
│ ├── src/
│ │ ├── main/
│ │ │ ├── java/com/example/helloworld/MainActivity.java
│ │ │ ├── res/layout/activity_main.xml
│ │ │ ├── res/values/strings.xml
│ │ │ └── AndroidManifest.xml
│ ├── build.gradle
├── build.gradle
└── settings.gradle
2. 关键文件代码示例
MainActivity.java
package com.example.helloworld;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置界面布局文件
setContentView(R.layout.activity_main);
}
}
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="24sp"
android:textColor="#000000"/>
</LinearLayout>
res/values/strings.xml
<resources>
<string name="app_name">HelloWorldApp</string>
<string name="hello_world">Hello World!</string>
</resources>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<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>
build.gradle (Module: app)
plugins {
id 'com.android.application'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.helloworld"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
}
settings.gradle
rootProject.name = "HelloWorldApp"
include ':app'
3. 快速启动指南
- 安装并打开 Android Studio
- 新建项目,选择 Empty Activity 模板
- 替换源码为上面给你的代码
- 连接手机或启动模拟器
- 点击运行,查看效果:屏幕中间显示 “Hello World!”
明白了!我帮你一步步搭建,从基础到进阶,覆盖:
- Hello World 完整项目(Java & Kotlin)
- 按钮点击事件示例
- 多界面跳转示例(Activity 切换)
- Kotlin 版本重写
- 简单网络请求示例(用 Retrofit)
1. Java 版 Hello World 完整项目结构和代码
(前面给你的我再稍微补充事件和多界面跳转)
MainActivity.java(带按钮事件跳转)
package com.example.helloworld;
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 btnGo = findViewById(R.id.btnGo);
btnGo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到 SecondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
activity_main.xml(带按钮)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textHello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="24sp"
android:textColor="#000000"/>
<Button
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转到第二个页面"
android:layout_marginTop="24dp"/>
</LinearLayout>
新建 SecondActivity.java
package com.example.helloworld;
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);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/textSecond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="欢迎来到第二个页面!"
android:textSize="24sp"
android:textColor="#000000"/>
</LinearLayout>
AndroidManifest.xml(添加 SecondActivity)
<activity android:name=".SecondActivity" />
2. Kotlin 版本的 Hello World + 按钮跳转
MainActivity.kt
package com.example.helloworld
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnGo.setOnClickListener {
startActivity(Intent(this, SecondActivity::class.java))
}
}
}
SecondActivity.kt
package com.example.helloworld
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
}
3. 简单网络请求示例(用 Retrofit)
添加依赖(build.gradle)
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
定义接口 ApiService.kt
package com.example.helloworld
import retrofit2.Call
import retrofit2.http.GET
interface ApiService {
@GET("todos/1")
fun getTodo(): Call<Todo>
}
data class Todo(
val userId: Int,
val id: Int,
val title: String,
val completed: Boolean
)
网络请求示例代码(MainActivity.kt)
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import retrofit2.*
import retrofit2.converter.gson.GsonConverterFactory
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val retrofit = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val service = retrofit.create(ApiService::class.java)
service.getTodo().enqueue(object : Callback<Todo> {
override fun onResponse(call: Call<Todo>, response: Response<Todo>) {
if (response.isSuccessful) {
val todo = response.body()
Toast.makeText(this@MainActivity, "标题: ${todo?.title}", Toast.LENGTH_LONG).show()
}
}
override fun onFailure(call: Call<Todo>, t: Throwable) {
Toast.makeText(this@MainActivity, "请求失败: ${t.message}", Toast.LENGTH_LONG).show()
}
})
}
}
4. 总结
- Java 和 Kotlin 都能做 Android 开发
- 基础界面和事件很简单,布局 XML 控制 UI
- 多界面跳转靠 Intent
- 网络请求推荐 Retrofit,支持异步调用和数据解析
发表回复