HarmonyOS实战开发-ArkUI组件(Button)

31 篇文章 0 订阅
31 篇文章 0 订阅

一、Button

Button(按钮)是一种常见的用户界面控件,通常用于触发操作或提交数据。Button 拥有文本标签和一个可点击的区域,用户点击该区域即可触发相应的操作或事件。

Button 的主要功能有:

  • 触发操作:用户点击 Button 可以触发相应的操作,例如提交表单、搜索、切换页面等。

  • 提交数据:Button 可以用于提交表单数据,将用户输入的数据提交到服务器进行处理。

  • 执行命令:Button 可以执行系统或应用程序的命令,例如打印、保存、退出等。

  • 触发事件:Button 可以触发自定义事件,通过与其他组件配合使用,可以实现复杂的交互效果。

1.创建按钮

语法说明:


Button(label?: string, options?: { type?: ButtonType, stateEffect?: boolean })
Button(options?: {type?: ButtonType, stateEffect?: boolean})

使用:

@Entry
@Component
struct Index {
 build() {
 Column(){
 Button('Ok', { type: ButtonType.Normal, stateEffect: true })
 .borderRadius(8)
 .backgroundColor(0x317aff)
 .width(90)
 .height(40)
 Button({ type: ButtonType.Normal, stateEffect: true }) {
 Row() {
 Image($r('app.media.app_icon')).width(20).height(40).margin({ left: 12 })
 Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
 }.alignItems(VerticalAlign.Center)
 }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
 }
 }
}

image.png

2.设置按钮类型


@Entry
@Component
struct Index {
 build() {
 Column(){
 Button('Disable', { type: ButtonType.Capsule, stateEffect: false })
 .backgroundColor(0x317aff)
 .width(90)
 .height(40)
 Button('Circle', { type: ButtonType.Circle, stateEffect: false })
 .backgroundColor(0x317aff)
 .width(90)
 .height(90)
 Button('Circle', { type: ButtonType.Normal, stateEffect: false })
 .backgroundColor(0x317aff)
 .width(90)
 .height(90)
 }
 }
}

image.png

注意:不支持通过borderRadius属性重新设置

3.自定义样式


@Entry
@Component
struct Index {
 build() {
 Column(){
 Button('circle border', { type: ButtonType.Normal })
 .borderRadius(20)
 .height(40)
 Button('font style', { type: ButtonType.Normal })
 .fontSize(20)
 .fontColor(Color.Pink)
 .fontWeight(800)
 Button('background color').backgroundColor(0xF55A42)
 Button({ type: ButtonType.Circle, stateEffect: true }) {
 Image($r('app.media.ic_public_refresh')).width(30).height(30)
 }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
 }
 }
}

image.png

4.添加事件


Button('Ok', { type: ButtonType.Normal, stateEffect: true }) 
 .onClick(()=>{ 
 console.info('Button onClick') 
 })

5.案例

Button按钮的实际应用场景主要包括以下几个方面:

点击提交表单

当用户填写完表单后,点击Button按钮来提交表单数据,使得数据能够被服务器端处理或者保存到数据库中。

跳转链接

当用户点击Button按钮时,跳转到指定的网页、应用程序或者其他页面。

打开或关闭弹窗

当用户点击Button按钮时,打开或关闭弹窗,可以在弹窗中展示一些信息、广告或者提示。

执行某个动作

当用户点击Button按钮时,执行某个操作,比如刷新页面、播放音乐、暂停视频等。

切换页面状态

当用户点击Button按钮时,可以切换页面的状态,比如打开或关闭菜单、切换语言、切换主题等。

Button按钮的应用场景非常广泛,基本上所有需要用户交互的场景都可以使用Button按钮来实现。

2.1 页面跳转

// xxx.ets
import router from '@ohos.router';
@Entry
@Component
struct ButtonCase1 {
 build() {
 List({ space: 4 }) {
 ListItem() {
 Button("First").onClick(() => {
 router.pushUrl({ url: 'pages/first_page' })
 })
 .width('100%')
 }
 ListItem() {
 Button("Second").onClick(() => {
 router.pushUrl({ url: 'pages/second_page' })
 })
 .width('100%')
 }
 ListItem() {
 Button("Third").onClick(() => {
 router.pushUrl({ url: 'pages/third_page' })
 })
 .width('100%')
 }
 }
 .listDirection(Axis.Vertical)
 .backgroundColor(0xDCDCDC).padding(20)
 }
}

image.png

2.2 表单提交

// xxx.ets
import router from '@ohos.router';
@Entry
@Component
struct Index {
 build() {
 Column() {
 TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
 TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
 Button('Register').width(300).margin({ top: 20 })
 .onClick(() => {
 // 需要执行的操作
 })
 }.padding(20)
 }
}

image.png

2.3 悬浮按钮

// xxx.ets
import router from '@ohos.router';
@Entry
@Component
struct Index {
 private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 build() {
 Stack() {
 List({ space: 20, initialIndex: 0 }) {
 ForEach(this.arr, (item) => {
 ListItem() {
 Text('' + item)
 .width('100%').height(100).fontSize(16)
 .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
 }
 }, item => item)
 }.width('90%')
 Button() {
 Image($r('app.media.ic_public_refresh'))
 .width(50)
 .height(50)
 }
 .width(60)
 .height(60)
 .position({x: '80%', y: 600})
 .shadow({radius: 10})
 .onClick(() => {
 // 需要执行的操作
 })
 }
 .width('100%')
 .height('100%')
 .backgroundColor(0xDCDCDC)
 .padding({ top: 5 })
 }
}

image.png

写在最后

有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)文档用来跟着学习是非常有必要的。

希望这一份鸿蒙学习文档能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!

如果你是一名有经验的资深Android移动开发、Java开发、前端开发、对鸿蒙感兴趣以及转行人员,可以直接领取这份资料

请点击→纯血版全套鸿蒙HarmonyOS学习文档

鸿蒙(HarmonyOS NEXT)5.0最新学习路线

在这里插入图片描述

路线图适合人群:

IT开发人员:想要拓展职业边界
零基础小白:鸿蒙爱好者,希望从0到1学习,增加一项技能。
技术提升/进阶跳槽:发展瓶颈期,提升职场竞争力,快速掌握鸿蒙技术

获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习文档

《鸿蒙 (HarmonyOS)开发入门教学视频》

在这里插入图片描述

《鸿蒙生态应用开发V3.0白皮书》

在这里插入图片描述

《鸿蒙 (OpenHarmony)开发基础到实战手册》

OpenHarmony北向、南向开发环境搭建

在这里插入图片描述

《鸿蒙开发基础》

●ArkTS语言
●安装DevEco Studio
●运用你的第一个ArkTS应用
●ArkUI声明式UI开发
.……
在这里插入图片描述

《鸿蒙开发进阶》

●Stage模型入门
●网络管理
●数据管理
●电话服务
●分布式应用开发
●通知与窗口管理
●多媒体技术
●安全技能
●任务管理
●WebGL
●国际化开发
●应用测试
●DFX面向未来设计
●鸿蒙系统移植和裁剪定制
……
在这里插入图片描述

《鸿蒙进阶实战》

●ArkTS实践
●UIAbility应用
●网络案例
……
在这里插入图片描述

获取以上完整鸿蒙HarmonyOS学习文档,请点击→纯血版全套鸿蒙HarmonyOS学习文档

在这里插入图片描述

  • 30
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值