本文专为 Android 入门小白打造,从概念 → 代码 → 运行效果,一次性讲透最常用的 7 个基础控件,看完就能上手实战。
前言
在 Android 开发里,界面 = 布局 + 控件。布局是 “架子”,控件是 “零件”。我们平时看到的文字、按钮、输入框、图片、选择项,全都是基础控件。这篇文章不讲复杂原理,只讲小白能听懂、能直接用的内容,带你快速入门。
一、先搞懂:什么是控件?
控件就是界面上能看到、能交互的基础元素。比如:显示文字、点击按钮、输入账号、选择性别、弹出提示等,都靠控件实现。
二、7 大基础控件详解
1. TextView 文本显示控件
作用:专门用来显示文字(标题、说明、内容)。
常用属性:
android:text:显示的文字android:textSize:文字大小(用 sp)android:textColor:文字颜色android:gravity:对齐方式
xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是文本控件"
android:textSize="18sp"
android:textColor="#ff0000"/>
运行效果:界面上显示一行红色文字。
2. Button 按钮控件
作用:可点击交互,触发操作(登录、提交、返回等)。
<Button
android:id="@+id/btn_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我试试"
android:background="#2196F3"
android:textColor="#ffffff"/>
Java 点击事件:
Button btnClick = findViewById(R.id.btn_click);
btnClick.setOnClickListener(v -> {
Toast.makeText(this, "你点击了按钮", Toast.LENGTH_SHORT).show();
});
运行效果:点击按钮弹出提示。
3. EditText 输入框控件
作用:让用户输入文字(账号、密码、搜索框)。
常用属性:
android:hint:提示文字android:inputType:输入类型(密码 / 数字 / 文本)
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入账号"
android:inputType="text"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"/>
运行效果:一个普通输入框 + 一个密码圆点输入框。
4. ImageView 图片控件
作用:显示图片(头像、图标、背景图)。
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_background"
android:scaleType="centerCrop"/>
运行效果:界面显示一张固定大小的图片。
5. RadioButton 单选按钮
作用:只能选一个(性别、单选选项)。必须放在 RadioGroup 里才能实现 “只能选一个”。
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
运行效果:男 / 女只能选一个。
6. CheckBox 复选框
作用:可以同时选多个(兴趣、爱好、多选)。
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看书"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="听歌"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"/>
运行效果:可同时勾选多个选项。
7. Toast 提示框
作用:弹出短暂提示,自动消失。⚠️ 注意:Toast 不在 XML 写,在 Java 代码里使用。
Toast.makeText(this, "操作成功!", Toast.LENGTH_SHORT).show();
运行效果:屏幕底部弹出提示,2 秒后消失。
三、7 大控件速记表
| 控件 | 作用 | 场景 |
|---|---|---|
| TextView | 显示文字 | 标题、说明 |
| Button | 可点击按钮 | 登录、提交 |
| EditText | 用户输入 | 账号、密码 |
| ImageView | 显示图片 | 头像、图标 |
| RadioButton | 单选 | 性别、选项 |
| CheckBox | 多选 | 兴趣、爱好 |
| Toast | 弹出提示 | 操作成功 / 提醒 |
四、完整实战 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:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Android 基础控件实战"
android:textSize="24sp"
android:textColor="#2196F3"
android:gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我弹出提示"
android:layout_marginTop="10dp"/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:layout_marginTop="10dp"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兴趣:"
android:layout_marginTop="10dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看书"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="听歌"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"/>
</LinearLayout>
五、小白必看避坑指南
- 图片名字只能用小写字母、数字、下划线
- 尺寸用
dp,文字用sp - 密码输入一定要用
textPassword - RadioButton 必须放在 RadioGroup 中
- Toast 最后一定要加
.show()
六、总结
Android 界面再复杂,都是由这些基础控件拼出来的。小白只要吃透这 7 个控件:显示、点击、输入、图片、单选、多选、提示,就能写出 80% 的基础界面。