IT评测·应用市场-qidao123.com
标题:
鸿蒙HarmonyOS实战-ArkUI组件(Button)
[打印本页]
作者:
圆咕噜咕噜
时间:
2024-6-8 09:37
标题:
鸿蒙HarmonyOS实战-ArkUI组件(Button)
一、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)
}
}
}
复制代码
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)
}
}
}
复制代码
留意:不支持通过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)
}
}
}
复制代码
4.添加事件
Button('Ok', { type: ButtonType.Normal, stateEffect: true })
.onClick(()=>{
console.info('Button onClick')
})
复制代码
5.案例
Button按钮的实际应用场景主要包罗以下几个方面:
点击提交表单
当用户填写完表单后,点击Button按钮来提交表单数据,使得数据能够被服务器端处理大概生存到数据库中。
跳转链接
当用户点击Button按钮时,跳转到指定的网页、应用程序大概其他页面。
打开或关闭弹窗
当用户点击Button按钮时,打开或关闭弹窗,可以在弹窗中展示一些信息、广告大概提示。
实行某个动作
当用户点击Button按钮时,实行某个操作,比如刷新页面、播放音乐、暂停视频等。
切换页面状态
当用户点击Button按钮时,可以切换页面的状态,比如打开或关闭菜单、切换语言、切换主题等。
Button按钮的应用场景非常广泛,基本上所有必要用户交互的场景都可以使用Button按钮来实现。
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4