跳到主要内容HarmonyOS6 RcButton 组件使用示例与最佳实践 | 极客日志TypeScript大前端
HarmonyOS6 RcButton 组件使用示例与最佳实践
档基于 HarmonyOS6 平台,全面展示 RcButton 组件的使用场景与最佳实践。内容包括基础类型(默认、主要、成功等)、镂空与文本样式、预设与自定义尺寸、方形圆角圆形形状、禁用与加载状态、颜色边框视觉定制、块级布局与组合、点击事件处理及节流控制。提供 ArkTS 代码示例,帮助开发者快速掌握组件用法并提升开发效率。
DotNetGuy26 浏览 文档说明
本文档基于 HarmonyOS6 平台,通过丰富的实际案例,全面展示 RcButton 组件的各种使用场景和最佳实践。每个示例都包含完整的代码和效果说明,帮助开发者快速掌握组件的使用方法。
一、基础使用

1.1 按钮类型展示
RcButton 提供了 6 种预设的按钮类型,满足不同场景的视觉需求。
import { RcButton, RcButtonType } from 'rchoui'
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '默认按钮', type: RcButtonType.DEFAULT })
RcButton({ text: '主要按钮', type: RcButtonType.PRIMARY })
RcButton({ text: '成功按钮', type: RcButtonType.SUCCESS })
RcButton({ text: '警告按钮', type: . })
({ : , : . })
({ : , : . })
}
RcButtonType
WARNING
RcButton
text
'错误按钮'
type
RcButtonType
ERROR
RcButton
text
'信息按钮'
type
RcButtonType
INFO
| 类型 | 枚举值 | 使用场景 | 视觉特点 |
|---|
| 默认按钮 | RcButtonType.DEFAULT | 普通操作、次要功能 | 灰色系,视觉权重低 |
| 主要按钮 | RcButtonType.PRIMARY | 主要操作、核心功能 | 蓝色系,视觉权重最高 |
| 成功按钮 | RcButtonType.SUCCESS | 成功提示、确认操作 | 绿色系,表示正向反馈 |
| 警告按钮 | RcButtonType.WARNING | 警告提示、需谨慎操作 | 橙色系,提示用户注意 |
| 错误按钮 | RcButtonType.ERROR | 危险操作、删除功能 | 红色系,强烈警示 |
| 信息按钮 | RcButtonType.INFO | 信息提示、辅助操作 | 青色系,中性提示 |
- 一个页面中主要按钮不宜超过 2 个,保持视觉焦点
- 删除等危险操作使用 ERROR 类型,并配合二次确认
- 表单提交使用 PRIMARY 类型,取消使用 DEFAULT 类型
1.2 镂空按钮
镂空按钮适用于需要降低视觉权重但仍需保持类型特征的场景。
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '默认镂空', plain: true })
RcButton({ text: '主要镂空', type: RcButtonType.PRIMARY, plain: true })
RcButton({ text: '成功镂空', type: RcButtonType.SUCCESS, plain: true })
RcButton({ text: '细边框', type: RcButtonType.PRIMARY, plain: true, hairline: true })
}
plain: true - 开启镂空模式,背景透明,显示边框和文字
hairline: true - 细线边框模式(0.5px),更加精致
- 背景色变为透明
- 文字颜色使用类型对应的主色
- 边框颜色使用类型对应的主色
- 按压时背景色显示为浅色
- 次要操作按钮(如'取消'按钮)
- 需要并列多个按钮且避免视觉过重
- 在深色背景上需要保持轻盈感
1.3 文本按钮
文本按钮是视觉权重最低的按钮形式,适合辅助性操作。
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '默认文本', textButton: true })
RcButton({ text: '主要文本', type: RcButtonType.PRIMARY, textButton: true })
RcButton({ text: '成功文本', type: RcButtonType.SUCCESS, textButton: true })
RcButton({ text: '错误文本', type: RcButtonType.ERROR, textButton: true })
}
- 无背景色,无边框
- 仅显示文字,占用空间最小
- 点击态为文字颜色加深
- 导航链接
- 工具栏小操作
- 表格行内操作
- 需要密集排列的操作按钮
二、尺寸控制
2.1 预设尺寸
RcButton 提供了 4 种预设尺寸,覆盖常见使用场景。
import { RcButtonSize } from 'rchoui'
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '大号按钮', btnSize: RcButtonSize.LARGE, type: RcButtonType.PRIMARY })
RcButton({ text: '普通按钮', btnSize: RcButtonSize.NORMAL, type: RcButtonType.PRIMARY })
RcButton({ text: '小号按钮', btnSize: RcButtonSize.SMALL, type: RcButtonType.PRIMARY })
RcButton({ text: '迷你按钮', btnSize: RcButtonSize.MINI, type: RcButtonType.PRIMARY })
}
| 尺寸 | 枚举值 | 高度 | 内边距 (横向) | 字号 | 使用场景 |
|---|
| 大号 | RcButtonSize.LARGE | 50px | 20px | 18px | 页面主要操作、移动端 |
| 普通 | RcButtonSize.NORMAL | 40px | 16px | 16px | 默认尺寸、通用场景 |
| 小号 | RcButtonSize.SMALL | 32px | 12px | 14px | 工具栏、表格操作 |
| 迷你 | RcButtonSize.MINI | 24px | 8px | 12px | 标签、筛选条件 |
- 移动端主按钮推荐使用 LARGE 尺寸,提升可点击性
- 桌面端表单推荐使用 NORMAL 尺寸
- 数据表格中的操作按钮推荐使用 SMALL 尺寸
- 标签类按钮推荐使用 MINI 尺寸
2.2 自定义尺寸
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '自定义宽高', btnWidth: 200, btnHeight: 60, type: RcButtonType.PRIMARY })
RcButton({ text: '大号文字', fontSize: 20, type: RcButtonType.SUCCESS })
RcButton({ text: '完全自定义', btnWidth: 180, btnHeight: 50, fontSize: 18, bordersRadius: 25, color: '#9b59b6' })
}
btnWidth: number - 按钮宽度(单位:vp)
btnHeight: number - 按钮高度(单位:vp)
fontSize: number - 文字大小(单位:fp)
bordersRadius: number - 圆角大小(单位:vp)
- 自定义宽高会覆盖
btnSize 设置
- 建议高度不低于 44px,保证触摸区域足够
- 圆角值不应超过高度的一半(圆形按钮除外)
三、形状变化
3.1 三种预设形状
RcButton 支持三种形状,满足不同设计风格需求。
import { RcButtonShape } from 'rchoui'
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '方形按钮', shape: RcButtonShape.SQUARE, type: RcButtonType.PRIMARY })
RcButton({ text: '圆角按钮', shape: RcButtonShape.ROUND, type: RcButtonType.PRIMARY })
RcButton({ text: '圆', shape: RcButtonShape.CIRCLE, type: RcButtonType.PRIMARY, btnWidth: 50, btnHeight: 50 })
}
| 形状 | 枚举值 | 圆角值 | 视觉效果 | 适用场景 |
|---|
| 方形 | RcButtonShape.SQUARE | 4px | 轻微圆角,现代感 | 后台管理、数据表格 |
| 圆角 | RcButtonShape.ROUND | 高度的一半 | 两端圆润,友好感 | 移动应用、社交类 APP |
| 圆形 | RcButtonShape.CIRCLE | 50% | 完全圆形,聚焦感 | 悬浮按钮、快捷操作 |
- 必须设置相等的宽高值
- 通常不设置文字,仅显示符号
- 适合作为页面悬浮操作按钮
- 建议尺寸:44×44、56×56、72×72
四、状态控制
4.1 禁用状态
禁用状态用于不可操作的场景,保持视觉存在但阻止交互。
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '默认禁用', disabled: true, textColor: '#000000' })
RcButton({ text: '主要禁用', disabled: true, textColor: '#000000' })
RcButton({ text: '镂空禁用', plain: true, disabled: true, textColor: '#000000' })
RcButton({ text: '文本禁用', textButton: true, disabled: true, textColor: '#000000' })
}.width('100%')
- 按钮不可点击
- 颜色变淡(透明度降低)
- 鼠标悬停无效果
- 不触发点击事件
@Component struct FormExample {
@State username: string = ''
@State password: string = ''
build() {
Column({ space: 20 }) {
TextInput({ placeholder: '请输入用户名' }).onChange((value) => this.username = value)
TextInput({ placeholder: '请输入密码', type: InputType.Password }).onChange((value) => this.password = value)
RcButton({
text: '登录',
type: RcButtonType.PRIMARY,
block: true,
disabled: !this.username || !this.password,
onBtnClick: () => {
console.log('执行登录')
}
})
}
}
}
4.2 加载状态
加载状态用于异步操作,提供视觉反馈,提升用户体验。
@Component struct LoadingExample {
@State loadingState: boolean = false
build() {
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '加载按钮', loading: true, type: RcButtonType.PRIMARY })
RcButton({ loadingText: '加载中...', loading: true, type: RcButtonType.SUCCESS })
RcButton({ loading: true, type: RcButtonType.WARNING })
RcButton({
text: this.loadingState ? '加载中...' : '点击加载',
loading: this.loadingState,
type: RcButtonType.PRIMARY,
onBtnClick: () => {
this.loadingState = true
setTimeout(() => {
this.loadingState = false
}, 2000)
}
})
}
}
}
- 显示旋转的加载动画
- 自动禁用按钮,阻止重复点击
- 可自定义加载时的文字提示
- 符号自动隐藏(避免布局冲突)
@Component struct AsyncOperationExample {
@State loading: boolean = false
@State resultMessage: string = ''
async submitData() {
this.loading = true
this.resultMessage = ''
try {
await new Promise((resolve) => setTimeout(resolve, 2000))
this.resultMessage = '提交成功!'
} catch (error) {
this.resultMessage = '提交失败,请重试'
} finally {
this.loading = false
}
}
build() {
Column({ space: 15 }) {
RcButton({
text: '提交数据',
loading: this.loading,
loadingText: '提交中...',
type: RcButtonType.PRIMARY,
block: true,
onBtnClick: () => this.submitData()
})
if (this.resultMessage) {
Text(this.resultMessage).fontSize(14).fontColor(this.resultMessage.includes('成功') ? '#52c41a' : '#ff4d4f')
}
}
}
}
五、视觉定制
5.1 自定义颜色
RcButton 支持完全自定义颜色方案,满足品牌定制需求。
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '自定义背景色', color: '#7232dd' })
RcButton({ text: '渐变色', color: 'linear-gradient(to right, #4254d8, #d533ba)' })
RcButton({ text: '自定义文字色', textColor: '#ff6b6b', plain: true })
RcButton({ text: '自定义边框色', bordersColor: '#00d4aa', plain: true })
RcButton({ text: '组合自定义', color: '#9b59b6', textColor: '#ffffff', bordersColor: '#8e44ad' })
}
| 属性 | 类型 | 说明 | 适用场景 |
|---|
color | string | ResourceColor | 背景颜色,支持纯色和渐变 |
textColor | string | ResourceColor | 文字颜色 |
bordersColor | string | ResourceColor | 边框颜色 |
- 十六进制:
'#ff0000'、'#f00'
- RGB:
'rgb(255, 0, 0)'
- RGBA:
'rgba(255, 0, 0, 0.5)'
- 线性渐变:
'linear-gradient(to right, #ff0000, #00ff00)'
- 资源引用:
$r('app.color.primary')
const BrandColors = {
primary: '#5B8FF9',
success: '#52C41A',
warning: '#FAAD14',
danger: '#F5222D',
gradient: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)'
}
RcButton({ text: '品牌主色', color: BrandColors.primary, type: RcButtonType.PRIMARY })
RcButton({ text: '渐变风格', color: BrandColors.gradient })
5.2 自定义边框
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '粗边框', plain: true, bordersWidth: 3, bordersColor: '#3c9cff', type: RcButtonType.PRIMARY })
RcButton({ text: '自定义圆角', bordersRadius: 20, type: RcButtonType.SUCCESS })
RcButton({ text: '完全方形', bordersRadius: 0, type: RcButtonType.WARNING })
RcButton({ text: '超大圆角', bordersRadius: 30, btnHeight: 60, type: RcButtonType.ERROR })
}
bordersWidth: number - 边框宽度(单位:vp),默认 1
bordersRadius: number - 圆角大小(单位:vp)
bordersColor: string | ResourceColor - 边框颜色
RcButton({ text: '细线边框', plain: true, hairline: true, type: RcButtonType.PRIMARY })
Row({ space: 10 }) {
RcButton({ text: '1px', plain: true, bordersWidth: 1 })
RcButton({ text: '2px', plain: true, bordersWidth: 2 })
RcButton({ text: '3px', plain: true, bordersWidth: 3 })
}
Row({ space: 10 }) {
RcButton({ text: '0', bordersRadius: 0, btnWidth: 60 })
RcButton({ text: '4', bordersRadius: 4, btnWidth: 60 })
RcButton({ text: '8', bordersRadius: 8, btnWidth: 60 })
RcButton({ text: '20', bordersRadius: 20, btnWidth: 60 })
}
六、布局应用
6.1 块级按钮
块级按钮占据父容器全部宽度,适合需要强调的主要操作。
Flex({ wrap: FlexWrap.Wrap, space: { main: LengthMetrics.px(10), cross: LengthMetrics.px(10) } }) {
RcButton({ text: '块级按钮', block: true, type: RcButtonType.PRIMARY })
RcButton({ text: '块级镂空', block: true, type: RcButtonType.SUCCESS, plain: true })
RcButton({ text: '块级文本', block: true, type: RcButtonType.WARNING, textButton: true })
}
- 宽度自动填充父容器
- 常用于移动端页面底部操作
- 适合表单提交按钮
- 可与其他块级按钮垂直堆叠
@Component struct MobileFooterExample {
build() {
Column() {
Scroll() {
Column({ space: 20 }) {
Text('页面内容...')
}
}.layoutWeight(1)
Column({ space: 12 }) {
RcButton({ text: '立即购买', block: true, type: RcButtonType.PRIMARY, btnHeight: 48, fontSize: 16 })
RcButton({ text: '加入购物车', block: true, plain: true, type: RcButtonType.PRIMARY, btnHeight: 48, fontSize: 16 })
}.padding({ left: 16, right: 16, bottom: 20 }).backgroundColor('#ffffff')
}.height('100%')
}
}
6.2 按钮组合
Row({ space: 10 }) {
RcButton({ text: '取消', type: RcButtonType.DEFAULT, plain: true, onBtnClick: () => { console.log('取消操作') } })
RcButton({ text: '确定', type: RcButtonType.PRIMARY, onBtnClick: () => { console.log('确定操作') } })
}.width('100%').justifyContent(FlexAlign.End)
Row({ space: 10 }) {
RcButton({ text: '编辑', icon: 'icon-houi_flash', type: RcButtonType.PRIMARY, btnSize: RcButtonSize.SMALL })
RcButton({ text: '删除', icon: 'icon-houi_tv', type: RcButtonType.ERROR, btnSize: RcButtonSize.SMALL })
RcButton({ text: '分享', icon: 'icon-houi_github', type: RcButtonType.SUCCESS, btnSize: RcButtonSize.SMALL })
}.width('100%')
Row({ space: 12 }) {
RcButton({ text: '取消', plain: true })
RcButton({ text: '确定', type: RcButtonType.PRIMARY })
}.width('100%').justifyContent(FlexAlign.End)
Row({ space: 8 }) {
RcButton({ text: '编辑', textButton: true, btnSize: RcButtonSize.SMALL })
RcButton({ text: '删除', textButton: true, btnSize: RcButtonSize.SMALL, type: RcButtonType.ERROR })
}
@State activeTab: number = 0
Row({ space: 0 }) {
RcButton({
text: '全部',
type: this.activeTab === 0 ? RcButtonType.PRIMARY : RcButtonType.DEFAULT,
plain: this.activeTab !== 0,
shape: RcButtonShape.SQUARE,
onBtnClick: () => this.activeTab = 0
})
RcButton({
text: '进行中',
type: this.activeTab === 1 ? RcButtonType.PRIMARY : RcButtonType.DEFAULT,
plain: this.activeTab !== 1,
shape: RcButtonShape.SQUARE,
onBtnClick: () => this.activeTab = 1
})
RcButton({
text: '已完成',
type: this.activeTab === 2 ? RcButtonType.PRIMARY : RcButtonType.DEFAULT,
plain: this.activeTab !== 2,
shape: RcButtonShape.SQUARE,
onBtnClick: () => this.activeTab = 2
})
}
七、交互功能
7.1 点击事件处理
RcButton 通过 onBtnClick 回调处理点击事件。
@Component struct ClickEventExample {
@State clickCount: number = 0
build() {
Column({ space: 15 }) {
Text(`点击次数:${this.clickCount}`).fontSize(16)
RcButton({
text: '点击我',
type: RcButtonType.PRIMARY,
onBtnClick: () => {
this.clickCount++
}
})
RcButton({
text: '重置计数',
type: RcButtonType.WARNING,
plain: true,
onBtnClick: () => {
this.clickCount = 0
}
})
}
}
}
onBtnClick?: (event: ClickEvent) => void
RcButton({ text: '提交', type: RcButtonType.PRIMARY, onBtnClick: () => {
if (this.validateForm()) {
this.submitForm()
}
}})
RcButton({ text: '查看详情', type: RcButtonType.PRIMARY, onBtnClick: () => {
router.pushUrl({ url: 'pages/Detail', params: { id: this.itemId } })
}})
RcButton({ text: '删除', type: RcButtonType.ERROR, onBtnClick: () => {
AlertDialog.show({
title: '确认删除',
message: '此操作不可恢复,确定要删除吗?',
primaryButton: { value: '取消', action: () => {} },
secondaryButton: { value: '确定', action: () => { this.deleteItem() } }
})
}})
7.2 节流控制
节流机制防止用户短时间内重复点击,保护接口和提升体验。
@Component struct ThrottleExample {
@State clickCount: number = 0
build() {
Column({ space: 15 }) {
Text(`点击次数:${this.clickCount}`).fontSize(16)
RcButton({
text: '普通点击',
type: RcButtonType.DEFAULT,
onBtnClick: () => {
this.clickCount++
console.log('点击时间:' + Date.now())
}
})
RcButton({
text: '节流按钮 (1 秒)',
type: RcButtonType.SUCCESS,
throttleTime: 1000,
onBtnClick: () => {
this.clickCount++
console.log('节流点击:' + Date.now())
}
})
RcButton({
text: '提交 (3 秒节流)',
type: RcButtonType.PRIMARY,
throttleTime: 3000,
onBtnClick: () => {
console.log('执行提交操作')
this.clickCount++
}
})
}
}
}
| 操作类型 | 推荐节流时间 | 说明 |
|---|
| 普通点击 | 300-500ms | 防止误触,保持流畅 |
| 数据提交 | 1000-2000ms | 防止重复提交 |
| 接口调用 | 1500-3000ms | 保护后端接口 |
| 支付操作 | 3000-5000ms | 防止重复扣款 |
- 首次点击立即触发回调
- 在节流时间内的后续点击被忽略
- 节流时间结束后,才能再次触发
- 不影响按钮的视觉反馈(按压态仍然正常)
@State submitting: boolean = false
RcButton({
text: '提交',
loading: this.submitting,
loadingText: '提交中...',
type: RcButtonType.PRIMARY,
throttleTime: 2000,
onBtnClick: async () => {
this.submitting = true
try {
await this.submitData()
promptAction.showToast({ message: '提交成功' })
} catch (error) {
promptAction.showToast({ message: '提交失败' })
} finally {
this.submitting = false
}
}
})
八、总结
RcButton 是一个功能强大且高度可定制的按钮组件,具有以下核心优势:
核心特性
- 6 种预设类型:覆盖常见业务场景
- 4 种尺寸规格:满足不同界面需求
- 3 种形状样式:适配多样设计风格
- 完整状态管理:禁用、加载、交互状态
- 高度可定制:颜色、尺寸、边框全面可控
- 性能优化:节流控制、状态联动
组件引入
import { RcButton, RcButtonType, RcButtonSize, RcButtonShape } from 'rchoui'
相关免费在线工具
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
- Markdown转HTML
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
- HTML转Markdown
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
- JSON 压缩
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
- JSON美化和格式化
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online