Button
按鈕組件,可快速創建不同樣式的按鈕。
說明:
開發前請熟悉鴻蒙開發指導文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
該組件從API Version 7開始支持。后續版本如有新增內容,則采用上角標單獨標記該內容的起始版本。
子組件
可以包含單個子組件。
接口
Button
Button(options?: {type?: ButtonType, stateEffect?: boolean})
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數:
參數名 | 參數類型 | 必填 | 參數描述 |
---|---|---|---|
type | ButtonType | 否 | 描述按鈕顯示樣式。 默認值:ButtonType.Capsule |
stateEffect | boolean | 否 | 按鈕按下時是否開啟按壓態顯示效果,當設置為false時,按壓效果關閉。 默認值:true**說明:**當開啟按壓態顯示效果,開發者設置狀態樣式時,會基于狀態樣式設置完成后的背景色再進行顏色疊加。 |
Button
Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
使用文本內容創建相應的按鈕組件,此時Button無法包含子組件。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數:
參數名 | 參數類型 | 必填 | 參數描述 |
---|---|---|---|
label | [ResourceStr] | 否 | 按鈕文本內容。 |
options | { type?: ButtonType, stateEffect?: boolean } | 否 | 見[Button]參數說明。 |
屬性
除支持[通用屬性]外,還支持以下屬性:
名稱 | 參數類型 | 描述 |
---|---|---|
type | ButtonType | 設置Button樣式。 默認值:ButtonType.Capsule 從API version 9開始,該接口支持在ArkTS卡片中使用。 |
stateEffect | boolean | 按鈕按下時是否開啟按壓態顯示效果,當設置為false時,按壓效果關閉。 默認值:true 從API version 9開始,該接口支持在ArkTS卡片中使用。 |
labelStyle10+ | [LabelStyle] | 設置Button組件label文本和字體的樣式。 |
ButtonType枚舉說明
從API version 9開始,該接口支持在ArkTS卡片中使用。
名稱 | 描述 |
---|---|
Capsule | 膠囊型按鈕(圓角默認為高度的一半)。 |
Circle | 圓形按鈕。 |
Normal | 普通按鈕(默認不帶圓角)。 |
說明:
LabelStyle10+對象說明
名稱 | 參數類型 | 必填 | 描述 |
---|---|---|---|
overflow | [TextOverflow] | 否 | 設置Label文本超長時的顯示方式。文本截斷是按字截斷。例如,英文以單詞為最小單位進行截斷,若需要以字母為單位進行截斷,可在字母間添加零寬空格。 |
maxLines | number | 否 | 設置Label文本的最大行數。默認情況下,文本是自動折行的,如果指定此參數,則文本最多不會超過指定的行。如果有多余的文本,可以通過textOverflow來指定截斷方式。 |
minFontSize | number | [ResourceStr] | 否 |
maxFontSize | number | [ResourceStr] | 否 |
heightAdaptivePolicy | [TextHeightAdaptivePolicy] | 否 | 設置Label文本自適應高度的方式。 |
font | [Font] | 否 | 設置Label文本字體樣式。HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿 |
事件
支持[通用事件]。
示例
示例1
// xxx.ets
@Entry
@Component
struct ButtonExample {
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
Text('Normal button').fontSize(9).fontColor(0xCCCCCC)
Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Button('OK', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(8)
.backgroundColor(0x317aff)
.width(90)
.onClick(() = > {
console.log('ButtonType.Normal')
})
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 })
}.alignItems(VerticalAlign.Center)
}.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(0xCCCCCC)
Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
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 })
}.alignItems(VerticalAlign.Center).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(0xCCCCCC)
Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
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)
}
}.height(400).padding({ left: 35, right: 35, top: 35 })
}
}
示例2
// xxx.ets
@Entry
@Component
struct SwipeGestureExample {
@State count: number = 0
build() {
Column() {
Text(`${this.count}`)
.fontSize(30)
.onClick(() = > {
this.count++
})
if (this.count <= 0) {
Button('count is negative').fontSize(30).height(50)
} else if (this.count % 2 === 0) {
Button('count is even').fontSize(30).height(50)
} else {
Button('count is odd').fontSize(30).height(50)
}
}.height('100%').width('100%').justifyContent(FlexAlign.Center)
}
}
示例3
// xxx.ets
@Entry
@Component
struct buttonTestDemo {
@State txt: string = 'overflowTextOverlengthTextOverflow.Clip';
@State widthShortSize: number = 200;
build() {
Row() {
Column() {
Button(this.txt)
.width(this.widthShortSize)
.height(100)
.labelStyle({ overflow: TextOverflow.Clip,
maxLines: 1,
minFontSize: 20,
maxFontSize: 20,
font: {
size: 20,
weight: FontWeight.Bolder,
family: 'cursive',
style: FontStyle.Italic
}
})
.fontSize(40)
}
.width('100%')
}
.height('100%')
}
}
審核編輯 黃宇
-
組件
+關注
關注
1文章
529瀏覽量
18292 -
Button
+關注
關注
0文章
16瀏覽量
7771 -
鴻蒙
+關注
關注
59文章
2507瀏覽量
43765
發布評論請先 登錄
鴻蒙ArkTS的起源和簡介
HarmonyOS/OpenHarmony應用開發-ArkTS的聲明式開發范式
HarmonyOS/OpenHarmony應用開發-ArkTS語言聲明式UI描述
鴻蒙ArkTS聲明式開發:跨平臺支持列表【按鍵事件】

鴻蒙ArkTS聲明式組件:PatternLock

評論