嘿,小朋友!或者正准备教小朋友的大人,你准备好了吗?我们要开始一场超级有趣的冒险啦!想象一下,你的手机就像是一个魔法盒子,里面住着各种各样的小精灵(也就是APP)。今天,我们要亲手做出第一个小精灵!
先认识一下我们的魔法工具
在我们开始之前,需要先准备好一些工具。这就像你要画画需要画纸和画笔一样。
你需要准备的魔法材料:
- 🖥️ 一台电脑(Windows、Mac或Linux都可以)
- 📱 一部Android手机(用来测试我们的作品)
- ⚡ 一点点耐心和时间
第一步:安装Android Studio
Android Studio是Google专门用来做Android APP的工具,它是免费的哦!就像你从商场里拿回来的免费礼物一样。
- 打开浏览器,搜索”Android Studio”
- 点击下载按钮
- 安装的时候,一直点”下一步”就可以了,就像玩闯关游戏一样简单
安装好了吗?太棒了!现在我们可以看到一个黑黑的界面,别害怕,我们只是要告诉它:”嘿,我要做一个新APP!”
创建你的第一个”Hello World”
还记得你说的第一句话是什么吗?”妈妈”或者”爸爸”?这次我们要说的第一句话是:”Hello, World!“(你好,世界!)
创建新项目的步骤:
- 打开Android Studio
- 点击”Start a new Android Studio project”(开始新项目)
- 在模板选择页面,选择”Empty Activity”(空页面)—— 这就好像选择了一张白纸开始画画
- 给你的项目取个名字吧!比如”MyFirstApp”(我的第一个APP)
- 选择”Language”为”Kotlin”—— 这是Google推荐的编程语言,就像是用魔法咒语来指挥手机
点击”Finish”(完成),然后等待它加载一会儿。这时候你可以去喝杯水,或者跟爸爸妈妈说:”我要开始创造世界啦!”
认识我们的魔法咒语:代码
打开项目后,你会看到很多文件。别担心,我们只需要关注两个最重要的文件:
- MainActivity.kt —— 这是大脑,告诉APP该做什么
- activity_main.xml —— 这是脸,告诉APP长什么样子
先看看”脸”长什么样
找到 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">
<!-- 这里可以放我们的按钮、文字、图片 -->
</androidx.constraintlayout.widget.ConstraintLayout>
这段话是什么意思呢?让我用小朋友能懂的方式解释:
<androidx.constraintlayout.widget.ConstraintLayout>就像是一个大盒子android:layout_width="match_parent"意思是这个盒子要填满整个屏幕android:layout_height="match_parent"意思是这个盒子要填满整个屏幕的高度
现在让我们在盒子里放东西!
我们要在盒子里放一个大大的文字,告诉大家:”Hello, World!”
修改 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">
<!-- 这是一个文字控件 -->
<TextView
android:id="@+id/hello_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="50sp"
android:textColor="#FF0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
哇!我们来一起看看这段代码说了什么:
android:id="@+id/hello_text"—— 给这个文字取个名字,这样我们以后可以找到它。就像你给你的玩具熊取名叫”小熊”一样。android:text="Hello, World!"—— 这就是我们要显示的文字啦!你会看到手机屏幕上出现”Hello, World!“这几个字。android:textSize="50sp"—— 这个数字越大,字就越大哦!你可以试试改成”30sp”或者”100sp”,看看会发生什么变化。android:textColor="#FF0000"—— 这是颜色的代码!”#FF0000”代表红色。如果你想改成蓝色,可以试试”#0000FF”;改成绿色,可以试试”#00FF00”。颜色代码就像调颜料一样好玩!app:layout_constraint...这几行代码是告诉文字:”你要站在屏幕的正中间哦!”Bottom_toBottomOf="parent"—— 底部对齐到父容器(屏幕)的底部Left_toLeftOf="parent"—— 左边对齐到父容器的左边Right_toRightOf="parent"—— 右边对齐到父容器的右边Top_toTopOf="parent"—— 顶部对齐到父容器的顶部
当这四个方向都对齐了,文字就会乖乖地站在正中间啦!
现在,让魔法启动!
代码写好了,但是手机还不知道要做什么呢。我们需要写”大脑”的代码,也就是 MainActivity.kt。
打开 MainActivity.kt 文件,你会看到一些代码,但别害怕,我们只需要加一点点魔法:
package com.example.myfirstapp
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 找到我们之前创建的文字控件
val helloText = findViewById<TextView>(R.id.hello_text)
// 当文字被点击的时候,显示一个消息
helloText.setOnClickListener {
Toast.makeText(this, "你点击了文字!好棒!", Toast.LENGTH_SHORT).show()
}
}
}
这段代码像是在跟手机说话:
setContentView(R.layout.activity_main)—— 告诉手机:”用刚才那个layout文件来显示界面”val helloText = findViewById<TextView>(R.id.hello_text)—— 这句话的意思是:”手机,帮我在界面上找到那个叫做’hello_text’的文字控件,把它给我”helloText.setOnClickListener { ... }—— 这是最神奇的部分!它在说:”当有人点击这个文字的时候,执行里面的事情”Toast.makeText(this, "你点击了文字!好棒!", Toast.LENGTH_SHORT).show()—— 这会在屏幕上弹出一个小小的提示框,显示”你点击了文字!好棒!”
运行你的第一个APP!
好了,所有的魔法咒语都念完了。现在让我们看看效果吧!
- 在Android Studio的顶部,你会看到一个绿色的三角形按钮 ▶️
- 点击它,然后选择你连接的手机(或者让它启动一个虚拟手机)
- 等待一会儿…
- 哇!你的第一个APP运行起来啦!
你会看到屏幕上显示大大的红色”Hello, World!“文字。当你点击它的时候,会出现一个提示框说”你点击了文字!好棒!”
这就是你的第一个Android APP!太厉害了!
让APP变得更有趣:加一个按钮
现在你已经知道怎么让文字显示了,我们来加一个按钮吧!按钮可以让APP变得更有趣。
修改 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">
<!-- 文字 -->
<TextView
android:id="@+id/hello_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="50sp"
android:textColor="#FF0000"
app:layout_constraintBottom_toTopOf="@id/change_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- 按钮 -->
<Button
android:id="@+id/change_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我改变颜色"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这次我们加了一个 <Button>,它和 <TextView> 很像,但是:
- 它是圆角的(Button天生就是圆角的)
- 它可以被点击
- 它里面写着”点我改变颜色”
在 MainActivity.kt 中,我们要让按钮工作:
package com.example.myfirstapp
import android.os.Bundle
import android.widget.TextView
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.util.Random
class MainActivity : AppCompatActivity() {
// 定义颜色数组
private val colors = intArrayOf(
0xFF0000.toInt(), // 红色
0x00FF00.toInt(), // 绿色
0x0000FF.toInt(), // 蓝色
0xFFFF00.toInt(), // 黄色
0xFF00FF.toInt(), // 粉色
0x00FFFF.toInt() // 青色
)
private var colorIndex = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 找到文字和按钮
val helloText = findViewById<TextView>(R.id.hello_text)
val changeButton = findViewById<Button>(R.id.change_button)
// 点击文字时的反应
helloText.setOnClickListener {
Toast.makeText(this, "你点击了文字!好棒!", Toast.LENGTH_SHORT).show()
}
// 点击按钮时改变文字颜色
changeButton.setOnClickListener {
// 切换到下一个颜色
colorIndex = (colorIndex + 1) % colors.size
// 改变文字颜色
helloText.setTextColor(colors[colorIndex])
// 显示提示
Toast.makeText(this, "颜色改变啦!", Toast.LENGTH_SHORT).show()
}
}
}
这里有一些新的东西:
private val colors = intArrayOf(...)—— 这就像是一个装着各种颜色的盒子(数组)。我们有红色、绿色、蓝色、黄色、粉色和青色。private var colorIndex = 0—— 这是一个计数器,告诉我们现在用的是哪个颜色。colorIndex = (colorIndex + 1) % colors.size—— 这是最有趣的数学!当点击按钮时,我们让计数器加1,然后用”取余数”的方法。这样颜色就会循环:红色→绿色→蓝色→黄色→粉色→青色→红色→绿色…
比如:
- 第一次点击:0 + 1 = 1,1 % 6 = 1,用第2个颜色(绿色)
- 第六次点击:5 + 1 = 6,6 % 6 = 0,用第1个颜色(红色)
这就像转圈圈一样,永远转不完!
加入图片:让APP更漂亮
文字和按钮已经很棒了,但是如果我们能加入图片,APP会更加生动!
首先,你需要准备一张图片。可以是:
- 你自己画的画
- 从网上下载的图片
- 拍的照片
把图片放到项目的 res/drawable 文件夹里。比如我们准备一张叫 happy_face.png 的图片。
然后在 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">
<!-- 图片 -->
<ImageView
android:id="@+id/happy_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/happy_face"
android:contentDescription="开心的表情"
app:layout_constraintBottom_toTopOf="@id/hello_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- 文字 -->
<TextView
android:id="@+id/hello_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="50sp"
android:textColor="#FF0000"
app:layout_constraintBottom_toTopOf="@id/change_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/happy_image" />
<!-- 按钮 -->
<Button
android:id="@+id/change_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我改变颜色"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里的新东西是 <ImageView>:
android:src="@drawable/happy_face"—— 指定要显示的图片android:contentDescription="开心的表情"—— 这是给看不见的人准备的描述,帮助他们了解图片是什么android:layout_width="200dp"和android:layout_height="200dp"—— 图片的大小是200x200像素
做一个真实的APP:猜数字游戏
现在你已经掌握了基础,让我们做一个真正的游戏!这个游戏很简单:手机随机想一个1到100之间的数字,你要猜出来。
设计游戏的思路
- 手机随机生成一个数字
- 玩家输入猜测的数字
- 告诉玩家”太大了”、”太小了”或者”猜对了!”
- 记录猜了多少次
- 猜对后,显示庆祝信息
完整的猜数字游戏代码
activity_main.xml:
”`xml
<?xml version=“1.0” encoding=“utf-8”?>
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"
android:background="#E0F7FA"
tools:context=".GuessNumberGame">
<!-- 标题 -->
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="🎮 猜数字游戏 🎮"
android:textSize="30sp"
android:textStyle="bold"
android:textColor="#006064"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="30dp" />
<!-- 游戏说明 -->
<TextView
android:id="@+id/instruction_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我想了一个1到100之间的数字,你能猜出来吗?"
android:textSize="18sp"
android:textColor="#00838F"
app:layout_constraintTop_toBottomOf="@id/title_text"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="20dp"
android:padding="10dp" />
<!-- 输入框 -->
<EditText
android:id="@+id/guess_input"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="输入你的猜测"
android:
