05:组件
组件是构建页面的核心,每个组件通过对数据和方法的简单封装,实现独立的可视、可交互功能单元。组件之间相互独立,随取随用,也可以在需求相同的地方重复使用。
本章主要讲述基于ArkTS
的声明式开发范式的相关组件。让大家能够了解常用组件的功能,使用组件进行UI界面的搭建,熟悉组件的核心属性和事件等。
组件通用信息
通用事件
- 点击事件
组件被点击时触发的事件。
// 点击动作触发该回调
onClick(event: (event?: ClickEvent) => void)
// src/main/ets/pages/common/Click.ets
@Entry
@Component
struct ClickExample {
@State text: string = ''
build() {
Column() {
Row({ space: 20 }) {
Button('Click', {type: ButtonType.Normal}).width(100).height(50)
.onClick((event: ClickEvent) => {
this.text = 'Click Point:' + '\n screenX:' + event.screenX + '\n screenY:' + event.screenY
+ '\n x:' + event.x + '\n y:' + event.y + '\ntarget:' + '\n width:'
+ event.target.area.width + '\n height:' + event.target.area.height + '\ntimestamp' + event.timestamp;
})
Button('Click', {type: ButtonType.Normal}).width(200).height(50)
.onClick((event: ClickEvent) => {
this.text = 'Click Point:' + '\n screenX:' + event.screenX + '\n screenY:' + event.screenY
+ '\n x:' + event.x + '\n y:' + event.y + '\ntarget:' + '\n width:'
+ event.target.area.width + '\n height:' + event.target.area.height + '\ntimestamp' + event.timestamp;
}).margin({top: 20})
}.margin(0)
Text(this.text).margin(15).fontSize(26)
}.width('100%').alignItems(HorizontalAlign.Start)
}
}
- 触摸事件
当手指在组件上按下、滑动、抬起时触发。
// 手指触摸动作触发该回调
onTouch(event: (event?: TouchEvent) => void)
// src/main/ets/pages/common/Touch.ets
@Entry
@Component
struct TouchExample {
@State text: string = ''
@State eventType: string = ''
build() {
Column() {
Button('Touch').height(40).width(100)
.onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) {
this.eventType = 'Down'
}
if (event.type === TouchType.Up) {
this.eventType = 'Up'
}
if (event.type === TouchType.Move) {
this.eventType = 'Move'
}
this.text = 'TouchType:' + this.eventType + '\nx: '
+ event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nwidth:'
+ event.target.area.width + '\nheight:' + event.target.area.height
})
Button('Touch').height(50).width(200).margin(20)
.onTouch((event: TouchEvent) => {
if (event.type === TouchType.Down) {
this.eventType = 'Down'
}
if (event.type === TouchType.Up) {
this.eventType = 'Up'
}
if (event.type === TouchType.Move) {
this.eventType = 'Move'
}
this.text = 'TouchType:' + this.eventType + '\nx: '
+ event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nwidth:'
+ event.target.area.width + '\nheight:' + event.target.area.height
})
Text(this.text).fontSize(26)
}.width('100%').padding(30)
}
}
- 按键事件
按键事件指组件与键盘、遥控器等按键设备交互时触发的事件(仅适用于所有可获焦组件,例如 Button)。
// 绑定该方法的组件获焦后,按键动作触发该回调
onKeyEvent(event: (event?: KeyEvent) => void)
// src/main/ets/pages/common/KeyEvent.ets
@Entry
@Component
struct KeyEventExample {
@State text: string = ''
@State eventType: string = ''
build() {
Column() {
Button('KeyEvent')
.onKeyEvent((event: KeyEvent) => {
if (event.type === KeyType.Down) {
this.eventType = 'Down'
}
if (event.type === KeyType.Up) {
this.eventType = 'Up'
}
this.text = 'KeyType:' + this.eventType + '\nkeyCode:' + event.keyCode + '\nkeyText:' + event.keyText
})
Text(this.text).padding(15).fontSize(26)
}.height(300).width('100%').padding(35)
}
}
通用属性
- 尺寸设置
用于设置组件的宽高、边距。
// src/main/ets/pages/common/Width.ets
// common/Width.ets
@Entry
@Component
struct WidthExample {
build() {
Column() {
Text("margin and padding:").fontSize(26).fontColor(0x3E3E3E).width('90%')
Text("锦瑟无端五十弦,一弦一柱思华年。").fontSize(26).fontColor(0xFFFFFF).width('90%').backgroundColor(Color.Blue)
Row() {
Row() {
Text("帝心").fontSize(26).fontColor(0x3E3E3E)
}
.height(260)
.backgroundColor(Color.Blue)
// width: 200, height: 200, padding: 10, margin: 20
Row() {
Row() {
Text("帝心帝心帝心帝心帝心帝心帝心帝心帝心帝心").fontSize(26).fontColor(0x3E3E3E).width('90%')
}
.width('100%')
.height('100%')
.backgroundColor(Color.Yellow)
}
.width(200)
.height(200)
.padding(20)
.margin(30)
.border({width: 10, color: Color.Red})
.backgroundColor(Color.Gray)
Row() {
Text("庄生").fontSize(26).fontColor(0x3E3E3E)
}
}
Text("庄生晓梦迷蝴蝶,望帝春心托杜鹃。").fontSize(26).fontColor(0xFFFFFF).width('90%').backgroundColor(Color.Blue)
}
}
}
- 位置设置
设置组件的对齐方式、布局方向和显示位置。
align: 设置元素内容在元素绘制区域内的对齐方式。
direction: 设置元素水平方向的布局。
// src/main/ets/pages/common/Position.ets
@Entry
@Component
struct PositionExample {
build() {
Column() {
Column({ space: 10 }) {
// 元素内容<元素宽高,设置内容在元素内的对齐方式
Text("align:").fontSize(26).fontColor(0x3E3E3E).width('90%')
Stack() {
Text('First').height('65%').backgroundColor(0xD2B48C)
Text('Second').backgroundColor(0xF5DEB3).opacity(0.9)
}.width('90%').height(50).margin({ top: 5 }).backgroundColor(0xFFE4C4)
.align(Alignment.TopStart)
// 父容器设置direction为Direction.Ltr,子元素从左到右排列
Text('direction').fontSize(26).fontColor(0x3E3E3E).width('90%')
Row() {
Text('1').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
Text('2').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
Text('3').height(50).width('25%').fontSize(16).backgroundColor(0xF5DEB3)
Text('4').height(50).width('25%').fontSize(16).backgroundColor(0xD2B48C)
}
.width('90%')
.direction(Direction.Ltr)
// 设置子组件左上角相对于父组件左上角的偏移位置
Text('position').fontSize(26).fontColor(0x3E3E3E).width('90%')
Row() {
Text('1').size({ width: '30%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
Text('2 position(30, 10)')
.size({ width: '60%', height: '30' })
.backgroundColor(0xbbb2cb)
.border({ width: 1 })
.fontSize(16)
.align(Alignment.Start)
.position({ x: 30, y: 10 })
Text('3').size({ width: '45%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
Text('4 position(50%, 70%)')
.size({ width: '50%', height: '50' })
.backgroundColor(0xbbb2cb)
.border({ width: 1 })
.fontSize(16)
.position({ x: '50%', y: '70%' })
}.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
// 相对于起点偏移,其中x为最终定位点距离起点水平方向间距,x>0往左,反之向右。
// y为最终定位点距离起点垂直方向间距,y>0向上,反之向下
Text('markAnchor').fontSize(26).fontColor(0x3E3E3E).width('90%')
Stack({ alignContent: Alignment.TopStart }) {
Row()
.size({ width: '100', height: '100' })
.backgroundColor(0xdeb887)
Text('1')
.size({ width: 25, height: 25 })
.backgroundColor(Color.Green)
.markAnchor({ x: 25, y: 25 })
Text('2')
.size({ width: 25, height: 25 })
.backgroundColor(Color.Yellow)
.markAnchor({ x: -100, y: -25 })
Text('3')
.size({ width: 25, height: 25 })
.backgroundColor(Color.Gray)
.markAnchor({ x: 25, y: -25 })
}.margin({ top: 25 }).border({ width: 1, style: BorderStyle.Dashed })
// 相对定位,x>0向右偏移,反之向左,y>0向下偏移,反之向上
Text('offset').fontSize(26).fontColor(0x3E3E3E).width('90%')
Row() {
Text('1').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
Text('2 offset(15, 30)')
.size({ width: 120, height: '50' })
.backgroundColor(0xbbb2cb)
.border({ width: 1 })
.fontSize(16)
.align(Alignment.Start)
.offset({ x: 15, y: 30 })
Text('3').size({ width: '15%', height: '50' }).backgroundColor(0xdeb887).border({ width: 1 }).fontSize(16)
Text('4 offset(-10%, 20%)')
.size({ width: 100, height: '50' })
.backgroundColor(0xbbb2cb)
.border({ width: 1 })
.fontSize(16)
.offset({ x: '-5%', y: '20%' })
}.width('90%').height(100).border({ width: 1, style: BorderStyle.Dashed })
}
}
.width('100%').margin({ top: 5 })
}
}
基础组件
Button
按钮组件,可快速创建不同样式的按钮。
方法1: Button(options?: {type?: ButtonType, stateEffect?: boolean})
方法2: Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
使用文本内容创建相应的按钮组件,此时Button无法包含子组件。
// src/main/ets/pages/basic/Button.etx
@Entry
@Component
struct ButtonExample {
build() {
Column({ space: 20 }) {
Text('Normal button').fontSize(9).fontColor(0x3E3E3E).fontSize(26)
Row({ space: 20 }) {
Button('OK', { type: ButtonType.Normal, stateEffect: true }).borderRadius(8).backgroundColor(0x317aff).width(90)
Button({ type: ButtonType.Normal, stateEffect: true }) {
Row() {
LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
}
}.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.4)
.borderRadius(8).backgroundColor(0x317aff).width(90)
}
Text('Capsule button').fontSize(9).fontColor(0x3E3E3E).fontSize(26)
Row({ space: 20 }) {
Button('OK', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
Button({ type: ButtonType.Capsule, stateEffect: true }) {
Row() {
LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
}.width(90).height(40)
}.backgroundColor(0x317aff)
Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.4)
.backgroundColor(0x317aff).width(90)
}
Text('Circle button').fontSize(9).fontColor(0x3E3E3E).fontSize(26)
Row({ space: 20 }) {
Button({ type: ButtonType.Circle, stateEffect: true }) {
LoadingProgress().width(20).height(20).color(0xFFFFFF)
}.width(55).height(55).backgroundColor(0x317aff)
Button({ type: ButtonType.Circle, stateEffect: true }) {
LoadingProgress().width(20).height(20).color(0xFFFFFF)
}.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
}.align(Alignment.Start).width('100%')
}.height(400).padding({ left: 35, right: 35, top: 35 })
}
}
Checkbox
提供多选框组件,通常用于某选项的打开或关闭。
Checkbox(options?: {name?: string, group?: string })
// src/main/ets/pages/basic/Checkbox.ets
@Entry
@Component
struct CheckboxExample {
@State checked: Object = {dx: true, zs: false}
build() {
Column({ space: 10 }) {
Text("checkbox:\n" + JSON.stringify(this.checked)).fontSize(26).fontColor(0x3E3E3E).width('90%')
Row() {
Checkbox({name: 'dx', group: 'checkboxGroup1'})
.select(true)
.selectedColor(0xed6f21)
.onChange((value: boolean) => {
console.info('帝心 change is: '+ value)
this.checked = {...this.checked, dx: value}
})
Text('帝心').fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500)
Checkbox({name: 'zs', group: 'checkboxGroup2'})
.select(false)
.selectedColor(0x39a2db)
.onChange((value: boolean) => {
console.info('庄生 change is: '+ value)
this.checked = {...this.checked, zs: value}
})
Text('庄生').fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500)
}
// 全选按钮
Text("checkbox group:\n帝心的特点有哪些").fontSize(26).fontColor(0x3E3E3E).width('90%')
Row() {
CheckboxGroup({ group: 'checkboxGroup' })
.selectedColor('#007DFF')
.onChange((itemName: CheckboxGroupResult) => {
console.info("checkbox group content" + JSON.stringify(itemName))
})
Text('Select All').fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500)
}
// 选项1
Row() {
Checkbox({ name: 'checkbox1', group: 'checkboxGroup' })
.selectedColor('#007DFF')
.onChange((value: boolean) => {
console.info('Checkbox1 change is: ' + value)
})
Text('真诚').fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500)
}.margin({ left: 36 })
// 选项2
Row() {
Checkbox({ name: 'checkbox2', group: 'checkboxGroup' })
.selectedColor('#007DFF')
.onChange((value: boolean) => {
console.info('Checkbox2 change is: ' + value)
})
Text('细').fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500)
}.margin({ left: 36 })
// 选项3
Row() {
Checkbox({ name: 'checkbox3', group: 'checkboxGroup' })
.selectedColor('#007DFF')
.onChange((value: boolean) => {
console.info('Checkbox3 change is: ' + value)
})
Text('帅').fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500)
}.margin({ left: 36 })
}
.margin(20)
}
}
DatePicker
日期选择器组件,用于根据指定日期范围创建日期滑动选择器。
// 根据指定范围的Date创建可以选择日期的滑动选择器。
DatePicker(options?: {start?: Date, end?: Date, selected?: Date})
// 选择日期时触发该事件。
onChange(callback: (value: DatePickerResult) => void)
// src/main/ets/pages/basic/DatePicker.ets
@Entry
@Component
struct DatePickerExample {
@State isLunar: boolean = false
private selectedDate: Date = new Date('2021-08-08')
build() {
Column() {
Button('切换 阳历/农历')
.margin({ top: 30, bottom: 30 })
.onClick(() => {
this.isLunar = !this.isLunar
})
DatePicker({
start: new Date('1970-1-1'),
end: new Date('2100-1-1'),
selected: this.selectedDate
})
.lunar(this.isLunar)
.onChange((value: DatePickerResult) => {
this.selectedDate.setFullYear(value.year, value.month, value.day)
console.info('select current date is: ' + JSON.stringify(value))
})
}.width('100%')
}
}