Android 入门教程
基础控件
本教程共 100 篇 · 第 59 篇 · 更新于 2026-07-28 · 约 7 分钟阅读
AndroidAndroid 入门教程TextViewButtonEditTextImageView控件
59. 基础控件
本节目标:掌握传统 View 体系的核心控件 TextView、Button、EditText、ImageView、CheckBox、RadioButton 的常用属性和代码操作。
这些控件在 Compose 里都有对应版本,但在传统 View 项目、或通过 AndroidView 嵌入时仍需要。这里精简讲核心用法。
TextView 文本
显示文字的最基础控件:
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题文字"
android:textColor="#333333"
android:textSize="16sp"
android:textStyle="bold"
android:maxLines="2"
android:ellipsize="end" />
常用属性:
| 属性 | 作用 |
|---|---|
text | 文字内容 |
textColor | 文字颜色 |
textSize | 字号(用 sp) |
textStyle | normal / bold / italic |
maxLines | 最大行数 |
ellipsize | 溢出处理:end(省略号)、marquee(跑马灯) |
gravity | 文字对齐 |
lineSpacingExtra | 行间距 |
代码操作:
binding.title.text = "新标题"
binding.title.setTextColor(Color.RED)
Button 按钮
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:enabled="true" />
点击事件:
binding.submit.setOnClickListener {
Toast.makeText(this, "点击了", Toast.LENGTH_SHORT).show()
}
Note
- Material Design 推荐用
MaterialButton替代原生Button,提供更好的样式和波纹效果。XML 中用<com.google.android.material.button.MaterialButton>。
EditText 输入框
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:inputType="text"
android:maxLength="20"
android:singleLine="true" />
inputType 输入类型
| 值 | 键盘类型 |
|---|---|
text | 普通文本 |
number | 数字 |
phone | 电话号码 |
textPassword | 密码(隐藏字符) |
textEmailAddress | 邮箱(有 @ 键) |
textMultiLine | 多行文本 |
监听输入变化
binding.username.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// 实时获取输入内容
val input = s.toString()
}
override fun afterTextChanged(s: Editable?) {}
})
Tip
TextWatcher有三个回调,最常用的是afterTextChanged,在文字变化完毕后触发。beforeTextChanged和onTextChanged大多数场景用不到。
ImageView 图片
<ImageView
android:id="@+id/avatar"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/default_avatar"
android:scaleType="centerCrop"
android:contentDescription="用户头像" />
scaleType 缩放模式
| 值 | 效果 |
|---|---|
centerCrop | 填满裁切(最常用) |
centerInside | 等比缩放居中 |
fitXY | 拉伸填满(会变形) |
fitCenter | 等比缩放居中 |
binding.avatar.setImageResource(R.drawable.new_avatar)
Note
- 加载网络图片不要直接用
setImageResource,那只能加载本地资源。网络图片用 Coil 或 Glide 库(第 90 章会讲)。contentDescription给无障碍读屏用,纯装饰图片设为@null。
CheckBox 复选框
<CheckBox
android:id="@+id/agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同意条款" />
binding.agree.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked) {
// 勾选了
} else {
// 取消了
}
}
RadioButton 单选按钮
单选需要放在 RadioGroup 里:
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
binding.genderGroup.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.male -> { /* 选了男 */ }
R.id.female -> { /* 选了女 */ }
}
}
Tip
RadioGroup保证组内只能选一个RadioButton。选了新的会自动取消旧的。checkedId就是选中的 RadioButton 的 id。
ProgressBar 进度条
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
水平进度条:
<ProgressBar
android:id="@+id/horizontalProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="30" />
binding.horizontalProgress.progress = 50
binding.horizontalProgress.visibility = View.VISIBLE
单位小结
| 单位 | 用途 | 说明 |
|---|---|---|
dp | 尺寸、间距 | 密度无关,不随字体缩放 |
sp | 字号 | 随系统字体大小缩放 |
px | 像素 | 不推荐直接用 |
小结
传统控件的核心是 TextView 显示文字、Button 按钮加点击、EditText 输入加监听、ImageView 显示图片配 scaleType、CheckBox 和 RadioGroup 做选择。用 ViewBinding 访问控件,用 inputType 控制键盘类型,用 scaleType 控制图片缩放。下一节学习 RecyclerView 列表。